smart developer's blog

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

XPath vs Linq to XML

with 2 comments

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><![CDATA[Necklace and earrings with pearl style stones and diamante.</br>
Available in a choice of colours.</br>
Lifetime plating guarantee.]]></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><![CDATA[Necklace and earring set with diamante and pearl style stones.</br>
Lifetime plating guarantee.]]></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

Written by smartdev

April 15th, 2009 at 4:11 pm

Posted in .Net,Programming

Tagged with , ,

2 Responses to 'XPath vs Linq to XML'

Subscribe to comments with RSS or TrackBack to 'XPath vs Linq to XML'.

  1. It is very interesting for me to read that blog. Thanx for it. I like such themes and anything that is connected to them. I would like to read more on that blog soon.

    Jane Swift
    jammers for sale

    Jane Swift

    5 Mar 11 at 1:59 am

  2. It’s difficult to find well-informed folks on this matter, but you appear to be guess what happens you happen to be talking about! Thanks

    Reggie Sjoquist

    4 Aug 11 at 9:09 am

Leave a Reply