XML (.NET 1.1) Performance Guidelines - XPath Queries

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman

Use XPathDocument to Process XPath Statements

If your application contains large amounts of intensive XPath or XSLT code, use XPathDocument, instead of XmlDataDocument or XmlDocument, to process XPath statements. However, a common scenario is to use XSLT to transform the default XML representation of a DataSet, in which case you can use the XmlDataDocument class for small-sized DataSet objects.

The XPathDocument class provides a fast, read-only cache for XML document processing by using XSLT. It provides an optimized in-memory tree structure that you can view by using the XPathNavigator interface. To move between a selected set of nodes by using an XPath query, use the XPathNodeIterator as shown in the following code.

  XPathDocument Doc = new XPathDocument(FileName);
  XPathNavigator nav = Doc.CreateNavigator();
  XPathNodeIterator Iterator = nav.Select("/bookstore/book");
  while (Iterator.MoveNext())

Avoid the // Operator by Reducing the Search Scope

Use path-specific XPath expressions, instead of the // operator, because the // operator performs a recursive descent and then searches the entire subtree for matches. Look for opportunities to reduce the search scope by restricting the search to specific portions of the XML subtree.

For example, if you know that a particular item only exists beneath a specific parent element, begin the search from that parent element and not from the root element. The following code fragment shows how to search an entire XML document and how to search beneath a specific element.

  XPathDocument doc = new XPathDocument("books.xml");
  XPathNavigator nav = doc.CreateNavigator();
  // this will search entire XML for matches
  XPathExpression Expr = nav.Compile("//price");
  // this will reduce the search scope
  XPathExpression Expr2 = nav.Compile("books/book/price");Compile Both Dynamic and Static XPath Expressions

The XPathNavigator class provides a Compile method that you can use to compile a string that represents an XPath expression. If you use the Select method repeatedly instead of passing a string each time, use the Compile method to compile and then reuse the XPath expression. The Compile method returns an XPathExpression object. The following code fragment shows how to use the Compile method.

  XPathDocument doc = new XPathDocument("one.xml");
  XPathNavigator nav = doc.CreateNavigator();
  XPathExpression Expr = nav.Compile("/invoices/invoice[number>20]");
  // Save Expr in application scope and reuse it
  XPathNodeIterator iterator = nav.Select(Expr);
  while (iterator.MoveNext())
    str = iterator.Current.Name;

You can also compile dynamic expressions. For more information, see MSDN article, "Adding Custom Functions to XPath," at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml10212002.asp.


For more information about using XPath expressions, see the following Microsoft Knowledge Base articles:

Personal tools