Today I simply want to call out a somewhat obscure but very useful method: XPathNavigator.Evaluate.
Many users of XML components are familiar with APIs such as SelectNodes or SelectSingleNodes. These are declared on the XmlNode class, and are thus available in its subclasses, for example XmlDocument and XmlElement.
The navigator class, however, provides a lot of functionality that isn’t exposed directly on document and elements. You can use this object to traverse the XML document, change it, validate it, write it out. The XPath functionality is largely built around the ‘traverse’ capabilities, and so there are a few of these APIs that take XPath expressions to operate on:
An interesting distinction between these is the result type: Select and SelectSingleNode will return either an iterator or a newly positioned navigator, while Matches will return a simple bool.
Evaluate, however, is typed to return an object, but as the MSDN documentation described, it can return the results of pretty much any XPath expression: booleans, numbers, text or node sets. This allows you to use XPath expressions which are easy to write and store to achieve interesting functionality.
Next time, how to leverage this for unit testing XML documents and processing components.