XPathNodeIterator Class (System.Xml.XPath)

Switch View :
ScriptFree
.NET Framework Class Library
XPathNodeIterator Class

Provides an iterator over a selected set of nodes.

Inheritance Hierarchy

System.Object
  System.Xml.XPath.XPathNodeIterator

Namespace:  System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)
Syntax

Visual Basic
Public MustInherit Class XPathNodeIterator _
	Implements ICloneable, IEnumerable
C#
public abstract class XPathNodeIterator : ICloneable, 
	IEnumerable
Visual C++
public ref class XPathNodeIterator abstract : ICloneable, 
	IEnumerable
F#
[<AbstractClass>]
type XPathNodeIterator =  
    class
        interface ICloneable
        interface IEnumerable
    end

The XPathNodeIterator type exposes the following members.

Constructors

  Name Description
Protected method XPathNodeIterator Initializes a new instance of the XPathNodeIterator class.
Top
Properties

  Name Description
Public property Count Gets the index of the last node in the selected set of nodes.
Public property Current When overridden in a derived class, returns the XPathNavigator object for this XPathNodeIterator, positioned on the current context node.
Public property CurrentPosition When overridden in a derived class, gets the index of the current position in the selected set of nodes.
Top
Methods

  Name Description
Public method Clone When overridden in a derived class, returns a clone of this XPathNodeIterator object.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Returns an IEnumerator object to iterate through the selected node set.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method MoveNext When overridden in a derived class, moves the XPathNavigator object returned by the Current property to the next node in the selected node set.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Extension Methods

  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method ICloneable.Clone For a description of this member, see XPathNodeIterator.Clone.
Top
Remarks

An XPathNodeIterator object returned by the XPathNavigator class is not positioned on the first node in a selected set of nodes. A call to the MoveNext method of the XPathNodeIterator class must be made to position the XPathNodeIterator object on the first node in the selected set of nodes.

When using the XPathNodeIterator, if you edit the current node or any of its ancestors, your current position is lost. If you want to edit a number of nodes that you have selected, create a XPathNavigator array, copy all of the nodes from the XPathNodeIterator into the array, then iterate through the array and modify the nodes.

There are two ways to iterate over an XPathNavigator collection by using the XPathNodeIterator class.

One way is to use the MoveNext method and then call Current to get the current XPathNavigator instance, as in the following example:

Visual Basic

While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
    Console.WriteLine(n.LocalName)
End While


C#

while (nodeIterator.MoveNext())
{
    XPathNavigator n = nodeIterator.Current;
    Console.WriteLine(n.LocalName);
}


Visual C++

        while (nodeIterator->MoveNext())
        {
            XPathNavigator^ n = nodeIterator->Current;
	    Console::WriteLine(n->LocalName);
        }


Another way is to use a foreach loop to call the GetEnumerator method and use the returned IEnumerator interface to enumerate the nodes, as in the following example:

Visual Basic

For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next


C#

foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);


Visual C++

        for each (XPathNavigator^ n in nodeIterator)
	    Console::WriteLine(n->LocalName);


You should either use MoveNext and Current or use GetEnumerator. Combining these two approaches can cause unexpected results. For example, if the MoveNext method is called first, and then the GetEnumerator method is called in the foreach loop, the foreach loop will not start enumerating the results from the beginning of the collection, but from the position after the Current method.

Notes to Inheritors

When you inherit from the XPathNodeIterator class, you must override the following members:

Examples

The following example uses the Select method of the XPathNavigator class to select a node set using the XPathNodeIterator class.

Visual Basic

Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim nodes As XPathNodeIterator = navigator.Select("/bookstore/book")
nodes.MoveNext()
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While


C#

XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
    Console.WriteLine(nodesText.Current.Value);


Visual C++

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathNodeIterator^ nodes = navigator->Select("/bookstore/book");
nodes->MoveNext();
XPathNavigator^ nodesNavigator = nodes->Current;

XPathNodeIterator^ nodesText = nodesNavigator->SelectDescendants(XPathNodeType::Text, false);

while (nodesText->MoveNext())
	Console::WriteLine(nodesText->Current->Value);


The example takes the books.xml file as input.


<?xml version="1.0" encoding="utf-8" ?> 
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference