Welcome to AspAdvice Sign in | Join | Help

From .NET Geek's Desk

Thoughts and Findings on .NET
LINQ To XML Overview

In this post I will discuss about how we can use the concepts of LINQ in parsing an xml document using the classes provided in the System.Xml.Linq namespace.We can think of any xml document as a collection of nodes where a node can be element,attribute,processing instruction,comment or text.Out of these some types of nodes are container nodes which can have child nodes and others cannot.The classes that are used as abstraction for different types of nodes are as follows:

  1. XNode - An abstract class representing an xml node
  2. XContainer - An abstract class representing an xml node that is a container for other nodes
  3. XDocument - Class representing a xml document
  4. XElement - Class representing a xml element
  5. XAttribute -Class representing a xml attribute

Let us consider the following sample xml:

<?xml version="1.0" encoding="utf-8" ?>
<PurchaseOrders>
  <PurchaseOrder PODate="12/31/2008" PONumber="1111"></PurchaseOrder>
  <PurchaseOrder PODate="12/31/2008" PONumber="2222"></PurchaseOrder>
  <PurchaseOrder PODate="12/31/2008" PONumber="3333"></PurchaseOrder>
</PurchaseOrders>

This xml document contains data for a collection of purchase orders.Each purchase order is represented as a child element of PurchaseOrders with attributes PO Date and PONumber.

First we need to load the xml document as shown below:

XDocument doc = XDocument.Load("Sample1.xml");

Now we need to take a closer look into the Descendants method of XDocument class.There are two overloads:

  • public IEnumerable<XElement> Descendants() - Returns a collection of descendant elements of a document or element.
  • public IEnumerable<XElement> Descendants(XName name) - Returns a collection of descendant elements of a document or element filtered by the element name passed parameter.The class XName represents a element or attribute name.This class has no public constructor but performs an implicit conversion from a string passed as value.

The following lines of code will return the child elements of PurchaseOrders.

IEnumerable<XElement> elist = from elem in doc.Descendants("PurchaseOrder")
                              select elem;

foreach (XElement el in elist)
{
    Console.WriteLine(el);
}

The output of the following code will be:


  <PurchaseOrder PODate="12/31/2008" PONumber="1111"></PurchaseOrder>
  <PurchaseOrder PODate="12/31/2008" PONumber="2222"></PurchaseOrder>
  <PurchaseOrder PODate="12/31/2008" PONumber="3333"></PurchaseOrder>

We can very easily use the anonymous types to convert the XML to an object represent as follows:

var polist = from elem in doc.Descendants("PurchaseOrder")
          select new
          {
              PONumber = elem.Attribute("PONumber"),
              PODate   = elem.Attribute("PODate")
          };

In the next post I will discuss about creating and modifying xml documents with LINQ.

Sponsor
Posted: Saturday, January 24, 2009 3:40 AM by sankarsan
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the code you see below

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS