I would like to admit that I have, many a times, taken XML handling for granted and have come with ghastly pieces of code like the one shown below, where all I need is to find out the number of nodes in an Xml document. What I am doing here, is loading the Xml data it to a DOM, creating a node list and then accessing its Count property – all in all, an extremely expensive and needless process.
A much better and more efficient way to solve this problem is to use XPath Expressions and the XPathNavigator class. The XPathNavigator provides a method called Evaluate which can evaluate Xpath expressions to return boolean, string, int values accordingly. As an alternative to the above code, we can just use the count Xpath function to return the number of nodes in the node-set.
The reason why the latter approach is efficient is that XPathDocument creates a read-only in-memory tree that is optimized for Xpath and Xslt. On the other hand, XmlNode is a modifiable DOM representation, which could make it inefficient for Xpath-like scenarios.
An example from Aaron Skonnard's Xml Files column illustrates this better. What the code sample is trying to do is find the sum price of invoice line items from the given Xml document.
The XmlDocument approach:
The better approach. Note the usage of the sum Xpath expression.