Thursday, August 7, 2008

LINQ for XML


var xml = XElement.Load (@"test.XML");
XNamespace ns = "http://test.com/stuff"

var query =
from node in xml.DescendantsAndSelf()
group node by node.Name into groupedNode
select new
{
Name = groupedNode.Key,
Occurs = groupedNode.Count()
};

query.Dump();


var
countryCrossTabulation =
from country in xml.DescendantsAndSelf(ns + "COUNTRY_NAME")
group country by country.Value into countryCrosstab
orderby countryCrosstab.Count() descending
select new
{
Name = countryCrosstab.Key,
Occurs = countryCrosstab.Count()
};

countryCrossTabulation.Dump()


No comments: