LINQ to XML 8211 really interesting

A lot of people tend to consider linq2sql the most important provider for linq, this is not true for me. First of all I tend to prefer NHibernate over Entity Framework and LINQ2SQL, and moreover I think that Linq 2 XML really rocks.

If you worked like me with XML you probably are frustrated of the many api you have to deal with: you have to know XPATH 1.0 and 2.0, XSL for transformation, DOM is useful too, and.NET really has a lot of classes to work with XML, and not all of them are really simple to use.

LINQ2XML solves a lot of problem, is a unifying API to get data from, create, and transform XML data. Let suppose you have such an XMl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<Customers>
  <customers>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <orders>
      <OrderID>10643</OrderID>
      <CustomerID>ALFKI</CustomerID>
   ...
    </orders>
    <orders>
     ...

This is the XML you can get from northwind database with a simple

1
2
3
select  * from customers
inner join orders on orders.customerID = customers.customerID
for xml AUTO, ELEMENTS, ROOT('Customers')

Now if you want to select id, name and orders number for all customer that has more than 10 orders you can do this query.

1
2
3
4
5
6
7
8
 var customers =
     from c in theDoc.Descendants(("customers"))    
    where c.Descendants("orders").Count() > 10
     select new {
        Id = (String)c.Element("CustomerID"),
        Name = (String)c.Element("CompanyName"),
        OrdersCount = c.Descendants("orders").Count()
    };

It is simple, it has nothing to do with XPATH or similar stuff, you have intellisense, and it works :D. If you want to transform that XML.. no more xsl pain :D

1
2
3
4
5
6
7
8
9
var customers = from c in theDoc.Descendants(("customers"))
   where c.Descendants("orders").Count() > 10
   select 
      new XElement("customer",
      new XAttribute("id", (String)c.Element("CustomerID")),
      new XAttribute("ordercount", c.Descendants("orders").Count()));

XDocument transDoc = new XDocument(
   new XElement("GoldCustomers", customers));

Whoa…… It seems to me that LINQ2SQL can really make my life better.

Alk.

Tags: LINQ LINQ2XML