smart developer’s blog

This is a C# resource library! Free how to’s and best practices…

Posts Tagged ‘xml

XPath vs Linq to XML

leave a comment »

Considering you have an xml file:


<Categories>
	<Category id="543" parentid="193" name="Bridal Jewellery & Accessories" treeactive="True">
		<Products>
		<Product id="6843" name="Pearl & Diamante Set">
			<Description><!&#91;CDATA&#91;Necklace and earrings with pearl style stones and diamante.</br>
Available in a choice of colours.</br>
Lifetime plating guarantee.&#93;&#93;></Description>
			<Stock>0</Stock>
			<Manufacturer>89</Manufacturer>
			<Supplier>A90/75043</Supplier>
			<Price>0.0000</Price>
			<RRP>0.0000</RRP>
			<CreationDate>01/01/0001 00:00:00</CreationDate>
			<Promo>False</Promo>
		</Product>
		<Product id="6844" name="Pearl & Diamante Set 2">
			<Description><!&#91;CDATA&#91;Necklace and earring set with diamante and pearl style stones.</br>
Lifetime plating guarantee.&#93;&#93;></Description>
			<Stock>0</Stock>
			<Manufacturer>89</Manufacturer>
			<Supplier>A90-75002</Supplier>
			<Price>0.0000</Price>
			<RRP>0.0000</RRP>
			<CreationDate>01/01/0001 00:00:00</CreationDate>
			<Promo>False</Promo>
		</Product>
	</Products>
	 </Category>
</Categories>

Basically there are two ways of querying XML files form C#:

1. The first one would be to use a XPath expresion. There are a few methods in System.Xml.XPath namespace (XPathEvaluate() which returns an object type, XPathSelectElement() which returns an XElement type and XPathSelectElemens() wich returns IEnumerable) that you can use.

For instance, in order to get all products containing “Pearl” in the product name, we will use something like this:


XmlReader reader = XmlReader.Create(filename);
XElement root = XElement.Load(reader);
reader.Close();
IEnumerable<XElement> children = root
				.XPathSelectElements("/Category/Products/Product[contains(@name,'Pearl')]");

2. Another way would be to use Linq to XML. The query is more complicated and not so compact as the XPath expression:


XmlReader reader = XmlReader.Create(filename);
XElement root = XElement.Load(reader);
reader.Close();
IEnumerable<XElement> children = root.Descendants("Category")
				.Descendants("Products")
				.Descendants("Product")
				.Where(o => o.Attribute("name").Value.Contains("Pearl"));

or


from xElem in root.Descendants("Category").Descendants("Products").Descendants("Product")
where xElem.Element("name").Value.Contains("Pearl")
select xElem;

I personally prefer to use XPath because it is more clearer. And sometimes there are queries that I couldn’t translate in Linq.

Lately, I was preocupied by the performance of each method and I was curios to find out which one is faster (for different sizes of xml files). So, I generated three xml files (small, medium and big) and tested 14 instructions on each file using both XPath and XML to Linq.

Unfortunatelly, we don’t have a clear result. Basically all times are the same no matter what we use – Linq or XPath. So, for now I guess I will stick to XPath only because it is more clearer :).

You can run the application here: http://seven.webme.ro/xpath/

xml_xpath

Advertisements

Written by smartdev

April 15, 2009 at 4:11 pm

Posted in .Net, Programming

Tagged with , ,