XPath Iterator
The XPathIterator class in the Windows API provides a mechanism for iterating over a set of nodes that match a given XPath expression.
Class Overview
This class is part of the System.Xml.XPath namespace and allows you to traverse through the nodes of an XML document that are selected by an XPath query. It implements the IEnumerator interface, providing standard iteration capabilities.
Methods
| Method Name | Description |
|---|---|
MoveNext() |
Advances the enumerator to the next node in the collection. Returns true if the enumerator was successfully advanced to the next node; returns false if the end of the collection has been reached. |
Reset() |
Sets the enumerator to its initial position, which is before the first node in the collection. |
Current |
Gets the current node in the iteration. This property returns a node of type XmlNode or a similar XML node representation. |
Example Usage
The following C# code snippet demonstrates how to use an XPathIterator to loop through all elements named "book" in an XML document:
using System;
using System.Xml;
using System.Xml.XPath;
// Assume 'xmlDoc' is an XmlDocument object loaded with your XML data.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(@"
Gambardella, Matthew
XML Developer's Guide
Computer
44.95
2000-10-01
An in-depth look at creating applications with XML.
Ralls, Kim
Midnight Rain
Fantasy
5.95
2000-12-16
A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
");
// Create an XPath navigator
XPathNavigator navigator = xmlDoc.CreateNavigator();
// Select the nodes using XPath
XPathNodeIterator iterator = navigator.Select("catalog/book");
Console.WriteLine("List of Books:");
while (iterator.MoveNext())
{
// Get the current node and extract its title
XPathNavigator currentNode = iterator.Current;
string title = currentNode.SelectSingleNode("title").Value;
Console.WriteLine($"- {title}");
}
Related Topics
Note: The
XPathNodeIterator is a fundamental component for processing XML data with XPath, enabling efficient traversal and manipulation of selected nodes.