Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing SOAP response with LINQ to XML - how to get nested nodes under parent?

I have an SOAP response that looks similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <getLoginResponse xmlns="http://<remotesite>/webservices">
        <person_id>123456</person_id>
        <person_name>John Doe</person_name>
    </getLoginResponse>
  </soapenv:Body>
</soapenv:Envelope>

I've been able to successfully extract the <get LoginResponse ...> node with the following LINQ code:

string soapResult = rd.ReadToEnd();

XNamespace ns = "http://<remotesite>/webservices";

XDocument xDoc = XDocument.Parse(soapResult);

var respUser = (from r in xDoc.Descendants(ns + "getLoginResponse")
                select new User
                           {
                               Name = r.Element("person_name").Value
                           }).FirstOrDefault();

However, the call to Name = r.Element("person_name").Value gives me an Object reference not set to an instance of an object error.

I looked into this further, and I see that if I run this query, all of the values (person_id, person_name) are in fact in the nested .Descendants().Descendants() XElement collection:

var respUser = (from r in xDoc.Descendants(ns + "getLoginResponse") 
                select r).Descendants().ToList();

So, what this tells me is in my original LINQ query, I am not extracting the nodes under <getLoginResponse> correctly.

How can I combine this together, using ... select new User { ... } to fill my custom object?

Doing something like:

var respUser = (from r in xDoc.Descendants(ns + "getLoginResponse").Descendants()
                select new User()
                              {
                                 Name = r.Element("person_name").Value
                              }).FirstOrDefault();

Doesn't work very well :)

Thanks all for the solution - I omitted the namespace from the child elements, which caused my issue!

like image 367
Evan Avatar asked Feb 07 '12 18:02

Evan


2 Answers

You need to add a valid namespace to your query, in your example that would be "http://foobar/webservices", e.g.:

XElement xml = XElement.Load(@"testData.xml");
XNamespace foobar = "http://foobar/webservices";
string personId = xml.Descendants(foobar + "person_id").First().Value;
like image 66
BrokenGlass Avatar answered Nov 14 '22 18:11

BrokenGlass


You need to include the namespace:

r.Element(ns + "person_name").Value
like image 31
SLaks Avatar answered Nov 14 '22 19:11

SLaks