Tip

XPath and XpathNodeIterator

 

XPath and XpathNodeIterator
Dale Michalk

It's a sure bet that Windows developers will be dealing with Microsoft's .NET initiative for a long time to come, which means you have to get your ducks in a row about .NET pretty quickly. This tip, excerpted from InformIT, is a follow-on to our last Windows developer tip; it discusses the relationship of function XpathNavigator and the XpathNodeIterator class.


XpathNavigator provides XPath support through the Select method. It takes an XPath statement as a parameter and returns a collection of nodes in the form of the XPathNodeIterator class. XPathNodeIterator implements the methods shown here to enumerate all matching nodes:

  • Count
  • Current
  • CurrentPosition
  • MoveNext

A special feature of XPathNavigator is the ability to return a simple scalar value from an XPath statement that returns a simple value using the Evaluate method. You pass an XPath statement that returns a value, and it compiles the statement, evaluates it and returns a result. You are responsible for casting the result to the appropriate type.

Listing 4 is a code demo that shows you how to take advantage of the XPath functionality. It takes an XPath statement that returns all the LineItem elements in the purchase order documents and passes that as a parameter to the Select statement of XPathNavigator to get an XPathNodeIterator. It uses the MoveNext method of XPathNodeIterator and the Current property to get XPathNavigator with its cursor set to the current node in the node set. Another highlight in the demo is the use of the GetAttribute method of XPathNavigator to gather the attribute value from an element node. After the navigation code is done, we show how the Evaluate method is used to return a simple scalar from an XPath query. The example uses an XPath query to get the sum of the number of items in the purchase order. Take a look at listing 5 to see the results.

Listing 4: XPath Demo

using System;
using System.Xml;
using System.Xml.XPath;

namespace XmlDotNet
{

public class XPathSelectDemo
{
   public static void Main()
   {
     // load the PO
     XPathDocument doc = new XPathDocument("PO.xml");

     // retrieve the navigator
     XPathNavigator nav = doc.CreateNavigator();
        
     // select all the LineItem elements
     Console.WriteLine("Items over $300");
     XPathNodeIterator nodes = 
        nav.Select("//LineItem[@Price > 300]");
        
     // move through each node in the result set
     // and display the current node
     while (nodes.MoveNext())
     {
        XPathNavigator node = nodes.Current;   
          
        Console.WriteLine("Name:{0} Price:{1}",
          node.GetAttribute("Name",""),
          node.GetAttribute("Price",""));
     }

     // use Evaluate to get the total number of items in the
     // order
     double Total = (double) 
        nav.Evaluate("sum(//LineItem/@Qty)");
     
     Console.WriteLine();
     Console.WriteLine("Number of Items: {0}",Total);
     
   }

}
}

Listing 5: XPath Demo Results

Items over $300
Name:Computer Desk Price:499.99
Name:Computer Price:999.99

Number of Items: 4

To read the rest of the article from which this tip is excerpted, click over to InformIT. You have to register there, but registration is free.


This was first published in July 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.