smart developer’s blog

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

XPath vs Linq to XML

leave a comment »

Considering you have an xml file:

	<Category id="543" parentid="193" name="Bridal Jewellery & Accessories" treeactive="True">
		<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>
			<CreationDate>01/01/0001 00:00:00</CreationDate>
		<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>
			<CreationDate>01/01/0001 00:00:00</CreationDate>

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);
IEnumerable<XElement> children = root

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);
IEnumerable<XElement> children = root.Descendants("Category")
				.Where(o => o.Attribute("name").Value.Contains("Pearl"));


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:



Written by smartdev

April 15, 2009 at 4:11 pm

Posted in .Net, Programming

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: