RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


XLinq Part 2: Using Standard Query Operators : Page 3

XLinq's query power goes way beyond the now-common methods of retrieving XML and querying it with XPath.

Transforming XML with XLinq
So far, you've seen how to query the XML file using query operators, but sometimes you might want to query the XML and transform the results directly into objects that you can then pass to other applications or services for further processing. With XLinq, converting XML to objects is a breeze, often requiring only a single line of code.

To demonstrate this, I'll show you how to query the Products.xml file and transform its contents into a collection of ProductInfo objects that are then bound to a GridView control. Listing 1 shows the complete code for the page, but here's the interesting portion. First, to retrieve all the <Product> elements inside the <Products> element, use the XElement object's Descendants() method:

   var productInfos =  from prod in
      orderby (string) prod.Element("Name")
      select new ProductInfo
         ProductID = (int)prod.Attribute("ID"),
         ProductName = (string)prod.Element("Name"),
       ProductNumber = (string)prod.Element("Number")                                
After you have all the <Product> elements, you then use those values to populate the ProductInfo collection. Here's the neat part: You can instantiate the ProductInfo objects and set their public properties as part of the select operator, using the new object initialization syntax available in the .NET Framework 3.0. Through this new syntax, you can instantiate a new object, set its properties to appropriate values, using—as promised—a single line of code.

As you can probably guess, the ProductInfo class used in the preceding example is very simple. It exposes three public properties: ProductID, ProductName, and ProductNumber.

   using System;
   public partial class ProductInfo
      private int _productID;
      private string _productName;
      private string _productNumber;
      public ProductInfo(){}
      public int ProductID
         get{return _productID;}
         set{_productID = value;}
Figure 4. XML-to-object Conversion: Here's the output produced by converting the products XML data to a ProductInfo collection and binding that to a GridView Control.
public string ProductName { get{return _productName;} set{_productName = value;} } public string ProductNumber { get{return _productNumber;} set{_productNumber = value;} } }
Place the ProductInfo class in the App_Code directory of your project, and then navigate to the page using the browser. You'll see the output shown in Figure 4.

In this installment of the XLinq series, you've seen how to use the standard LINQ query operators to select, and filter XML data, as well as how to transform XML data into a strongly-typed collection. The next installment focuses on using DLinq (LINQ to SQL) in conjunction with XLinq to retrieve relational data, transform it to XML data, and vice versa.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date