RattyLaa
RattyLaa

Reputation: 323

LINQ to XML getting XElement values

I am having an issue with getting some values back from LINQ to XML query. I have got the XML from a SOAP web service and passed this through and parsed this into an XDocument to query

The XML:

<?xml version="1.0" encoding="utf-8"?>
<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>
<GetCollectionByUprnAndDateResponse xmlns=\"http://webservices.whitespacews.com/\">
  <GetCollectionByUprnAndDateResult>
    <ErrorCode>0</ErrorCode>
    <ErrorDescription>Success</ErrorDescription>
    <SuccessFlag>true</SuccessFlag>
    <Collections>
      <Collection>
        <Service>Garden Waste Collection Service</Service>
        <Round>RR3 GDN</Round>
        <Schedule>TueFort2</Schedule>
        <Day>Tuesday</Day>
        <Date>01/04/2014</Date>
      </Collection>
      <Collection>
        <Service>Recycling Collection Service</Service>
        <Round>RR8 REC</Round>
        <Schedule>TueFort2</Schedule>
        <Day>Tuesday</Day>
        <Date>01/04/2014</Date>
      </Collection>
      <Collection>
        <Service>Refuse Collection Service</Service>
        <Round>RR8 REF</Round>
        <Schedule>TueFort1</Schedule>
        <Day>Tuesday</Day>
        <Date>08/04/2014</Date>
      </Collection>
      <Collection>
        <Service>Garden Waste Collection Service</Service>
        <Round>RR3 GDN</Round>
        <Schedule>TueFort2</Schedule>
        <Day>Tuesday</Day>
        <Date>15/04/2014</Date>
      </Collection>
      <Collection>
        <Service>Recycling Collection Service</Service>
        <Round>RR8 REC</Round>
        <Schedule>TueFort2</Schedule>
        <Day>Tuesday</Day>
        <Date>15/04/2014</Date>
      </Collection>
      <Collection>
        <Service>Refuse Collection Service</Service>
        <Round>RR8 REF</Round>
        <Schedule>TueFort1</Schedule>
        <Day>Tuesday</Day>
        <Date>22/04/2014</Date>
      </Collection>
    </Collections>
  </GetCollectionByUprnAndDateResult>
</GetCollectionByUprnAndDateResponse>
</soap:Body>
</soap:Envelope>

The CS Code:

using (var reader = new StreamReader(response.GetResponseStream()))
{
   string xmlString = reader.ReadToEnd();
   XDocument xmlDoc = XDocument.Parse(xmlString);
   var collections = (from p in xmlDoc.Descendants()
       where p.Name.LocalName == "Collection"
       select p);

   var test = xmlDoc.Descendants("Collection").Select(
       x => new
       {
           day = x.Element("Day").Value,
           date = x.Element("Date").Value
       });
       Debug.WriteLine("STOP HERE");

   var test2 = (from p in xmlDoc.Descendants()
       where p.Name.LocalName == "Collection"
       select new{
          day = p.Element("Day").Value,
          date = p.Element("Date").Value
       });

}

The collections var above gives me a list of the nodes, however the test and test2 var's does not get get me the child elements.

any help appreciated!

Upvotes: 0

Views: 639

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503270

This is what's causing you the headache:

xmlns="http://webservices.whitespacews.com/"

That's setting the default namespace for the element and its descendants - so you need to look for elements with a local name of Collections and that namespace. Ditto the other elements. Fortunately, LINQ to XML makes that easy for you:

XNamespace ns = "http://webservices.whitespacews.com/";
...
var test = xmlDoc.Descendants(ns + "Collection")
                 .Select(x => new
                 {
                     day = x.Element(ns + "Day").Value,
                     date = x.Element(ns + "Date").Value
                 });

I suspect your test2 collection would have elements, but because it won't be able to find the Day and Date elements, so fetching the Value property will result in a NullReferenceException.

Also note that there's no need for you to create your own reader etc. I would write your code as:

XNamespace ns = "http://webservices.whitespacews.com/";
XDocument doc;
using (var stream = response.GetResponseStream())
{
    doc = XDocument.Load(stream);
}
var query = xmlDoc.Descendants(ns + "Collection")
                  .Select(x => new
                  {
                      Day = x.Element(ns + "Day").Value,
                      Date = x.Element(ns + "Date").Value
                  });

Upvotes: 1

Related Questions