Using C# and XDocument/XElement to parse a Soap Response

31,616

Solution 1

You might want to try something like this:

string myNamespace= "http://mycrazyservice.com/SuperDuperService";

var results = from result in yourXml.Descendants(XName.Get("MyResponse", myNamespace))
              select result.Element("Result").value

Don't have VS on this laptop so I can't double check my code, but it should point you in the right direction using LINQ to SQL.

Solution 2

to extend Justin's answer with tested code with a return that excpects a boolean and that the response and result start with the method name (BTW - a surprise is even thought the XML element does not show the NS it requires it when parsing):

    private string ParseXml(string sXml, string sNs, string sMethod, out bool br)
    {
        br = false;
        string sr = "";
        try
        {
            XDocument xd = XDocument.Parse(sXml);

            if (xd.Root != null)
            {
                XNamespace xmlns = sNs;
                var results = from result in xd.Descendants(xmlns + sMethod + "Response")
                              let xElement = result.Element(xmlns + sMethod + "Result")
                              where xElement != null
                              select xElement.Value;
                foreach (var item in results)
                    sr = item;
                br = (sr.Equals("true"));
                return sr;
            }
            return "Invalid XML " + Environment.NewLine + sXml;
        }
        catch (Exception ex)
        {
            return "Invalid XML " + Environment.NewLine + ex.Message + Environment.NewLine + sXml;
        }
    }

Solution 3

Maybe like this:

IEnumerable<XElement> list = doc.Document.Descendants("Result");
if (list.Count() > 0)
{
    // do stuff
}

Solution 4

You're searching in the right direction, it definitely has to do with the namespace.

The code below returns the first element found for the combination of namespace and element name.

XDocument doc = XDocument.Load(@"c:\temp\file.xml");
XNamespace ns = @"http://mycrazyservice.com/SuperDuperService";
XElement el = doc.Elements().DescendantsAndSelf().FirstOrDefault( e => e.Name == ns + "Result");
Share:
31,616
strickland
Author by

strickland

Senior Software Developer

Updated on July 09, 2022

Comments

  • strickland
    strickland almost 2 years

    Here is a sample soap response from my SuperDuperService:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <MyResponse xmlns="http://mycrazyservice.com/SuperDuperService">
                <Result>32347</Result> 
            </MyResponse>
        </soap:Body>
    </soap:Envelope>
    

    For some reason when I try to grab the Descendant or Element of "Result" I get null. Does it have something to do with the Namespace? Can someone provide a solution to retrieve Result from this?

  • BeYourOwnGod
    BeYourOwnGod almost 13 years
    For some reason the list.Count() method from your example above does not exist for me?? Im using VS2010 .net 4.
  • Sagotharan
    Sagotharan over 9 years
    got Error - A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else Error on This solution. How can I solve that
  • Carlos de Jesus Baez
    Carlos de Jesus Baez almost 4 years
    XDocument does not contain a definition for 'Parse'
  • azpc
    azpc almost 4 years
    System.Xml.Linq.XDocument.Parse does indeed exist in Assembly: System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089