ChethanRao
ChethanRao

Reputation: 191

Unable to find root node while parsing xml using XElement - Windows Phone 7

I have moved into parsing XML files in WP7 and until now was finding it quite straightforward. My current XML is something like this :

                         <Node1 attrib1="abc" attrib2="def">
                              <Node2>
                                   <Node3>
                                   </Node3>
                              <Node2>
                         <Node1>

As you can see, my root node itself has some attributes and I intend to access them, but with this code I am NOT able to do that :

            streamResult = myXMLState.AsyncXMLResponse.GetResponseStream();
            XElement myXml = XElement.Load(streamResult);
            var parse = from feed in myXml.Descendants("Node1")
                                   select new MyCustomDataType
                                   {
                                     Attribute1 = feed.Attribute("attrib1").Value,
                                     Attribute2 = feed.Attribute("attrib2").Value,
                                   };

The size of variable "parse" always returns 0 here. Am I doing something wrong. Appreciate ur help, as always!!

Upvotes: 0

Views: 2056

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502056

You're asking for the descendants of the element - which won't include the element itself.

Either load it as a document instead (where the root node will count as a descendant of the document) or use DescendantsAndSelf.

Upvotes: 2

Related Questions