Pranay Rana: Read Xml with Descendants Method (XName)

Wednesday, November 7, 2012

Read Xml with Descendants Method (XName)

This post is about understanding Descendants and avoid misconception with this method.
Recently I read one question on StackOverFlow  about reading xml using Linq To Xml to get node values. In that developer made use of Descendants Method to get the child node values.

Let see the actual problem here following is XML to read. And developer wrote code to read out value of orderid node.
<ordersreport date="2012-08-01">
<returns>
      <amount>
        <orderid>2</orderid>
        <orderid>3</orderid>
        <orderid>21</orderid>
        <orderid>23</orderid>
      </amount>
    </returns>
</ordersreport>
So code written like this
    var amount = documentRoot.Descendants("Amount")
               .Select(y => new
               {
                  OrderId = (int)y.Element("OrderId")
               });
               foreach (var r in amount)
               {
                  Console.WriteLine(r.OrderId);
               }
Ouput of above code is
 2
that is only first orderid element value which is child of Amount , So misconception here by developer of the code is Descendants("Amount") returns child element of the Amount tag i.e. all orderId element.

Now to Understand Descendants function in better way I visited to MSDN link which says something like this
XContainer.Descendants Method (XName) - Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection. So as per the defincation on MSDN problem with code
    var amount = doc.Descendants("Amount")                         
      .Select(y => new
      {
       OrderId = (int)y.Element("OrderId")
       });
will give you Element Amount and when you write y.Element("OrderId") will return you fist element of its child.
Descendants - doesn't mean than its return the child element of element name rather than method look for descendants of element or if name of elemnt specified as parameter than matching descendants.
Finally I got following solution to get it properly work
XElement documentRoot  = 
     XElement.Parse (@"<ordersreport date="2012-08-01">
                             <returns>
                              <amount>
                                  <orderid>2</orderid>                                                    
                                  <orderid>3</orderid>
                                  <orderid>21</orderid>
                                  <orderid>23</orderid>
                               </amount>
                             </returns>
                        </ordersreport>");
Solution 1
var orderids = from order in
                  documentRoot.Descendants("Amount").Descendants()
                  select new
                  {
                     OrderId = order.Value
                  };
As per the information on MSDN documentRoot.Descendants("Amount").Descendants() give list of orderId elements.

Solution 2
var orderids = from order in
                    documentRoot.Descendants("OrderId")
                    select new
                    {
                       OrderId = order.Value
                    };
or the second solution is just bit easy than this just make use of documentRoot.Descendants("OrderId") that will give all orderidelement.

Conclusion
This post is just for avoiding misconception related to Descendants and understand it properly.

Leave your comments if you like it.

No comments:

Post a Comment