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

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.


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;
        }
    }

Tags:

C#

Xml

Parsing

Soap