Section 5.8. Select Within XPathDocument Using XPath


5.8. Select Within XPathDocument Using XPath

You can select a portion of the XML document to feed to XPathNavigator to limit the area of your XML document that is parsed or to enhance the efficiency of your application.

To do so, you'll use the SelectNode( ) or SelectNodes( ) methods of the XPathDocument. These methods require an XPath expression to identify which nodes you are looking for.


Note: Simplify your use of XPathNavigator by selecting a portion of the XmlDocument with XPath.

5.8.1. How do I do that?

Make a copy of your previous lab and name it XPathTester. Replace the Find_Click button handler with the code in Example 5-6.


Tip: The simplest way to do this is to copy the directory, rename the new directory, throw away the solution file, open the project, and rename the project. Then edit the code.
Example 5-6. Selecting nodes with XPath
private void btnFind_Click(object sender, EventArgs e) {     int numAuthors = 0;     string authorName = string.Empty; ;          System.Xml.XPath.XPathDocument document =         new XPathDocument("..\\..\\BookList.xml");     XPathNavigator navigator = document.CreateNavigator( );          // selection with filter     string selectionCriteria = "//Author[../BookName='" + txtBookName.Text + "']";          XPathExpression query = navigator.Compile(selectionCriteria);          // select the matching nodes     XPathNodeIterator nodes = navigator.Select(query);          while (nodes.MoveNext( ))     {         if (++numAuthors > 1)   // make a list         {             authorName += ", ";         }         authorName += nodes.Current.Value; // nodeIterator.Current.Value;     }          if (numAuthors =  = 0)     {         authorName = "Not Found";     }     lblAuthor.Text = authorName; }


Tip: When running, the application looks identical to the previous example.

5.8.2. What just happened?

In this example, instead of looking through all the nodes, you use XPath to find the Author node. You filter that Author node to say that you only want the Author nodes which are within nodes where BookName matches the book name the user entered:

string selectionCriteria = "//Author[../BookName='" + txtBookName.Text + "']";

You pass that string to the Compile method of the navigator, which returns an XPathExpression:

XPathExpression query = navigator.Compile(selectionCriteria);

You feed that XPathExpression to the Select method of the XPathNavigator which returns an XPathNodeIterator that you can use to iterate over the matching nodes:

XPathNodeIterator nodes = navigator.Select(query);

5.8.3. What about...

...retrieving the data values as strongly typed values? Can I do that?

Yes, you can do that. Instead of using the string value returned by the Value property, you can use one of the strongly typed properties.

These include:

  • ValueAsBoolean

  • ValueAsDateTime

  • ValueAsDecimal

  • ValueAsDouble

  • ValueAsInt32

  • ValueAsInt64

  • ValueAsList

  • ValueAsSingle

5.8.4. Where can I learn more?

XPath is a big topic, and tutorials are available that are dedicated to teaching how to use XPath well. One good starting point is http://www.w3schools.com/. Another tutorial is available at http://java.sun.com/webservices/docs/1.3/tutorial/doc/JAXPXSLT3.html.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net