Прочитај на енглеском Уреди

Делите путем


XPathNodeIterator Class

Definition

Provides an iterator over a selected set of nodes.

C#
public abstract class XPathNodeIterator : ICloneable, System.Collections.IEnumerable
C#
public abstract class XPathNodeIterator : System.Collections.IEnumerable
C#
public abstract class XPathNodeIterator : ICloneable
Inheritance
XPathNodeIterator
Implements

Examples

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

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);

The example takes the books.xml file as input.

XML
<?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>  

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:

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:

C#
foreach (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 Implementers

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

Constructors

XPathNodeIterator()

Initializes a new instance of the XPathNodeIterator class.

Properties

Count

Gets the index of the last node in the selected set of nodes.

Current

When overridden in a derived class, gets the XPathNavigator object for this XPathNodeIterator, positioned on the current context node.

CurrentPosition

When overridden in a derived class, gets the index of the current position in the selected set of nodes.

Methods

Clone()

When overridden in a derived class, returns a clone of this XPathNodeIterator object.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

Returns an IEnumerator object to iterate through the selected node set.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
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.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICloneable.Clone()

Creates a new object that is a copy of the current instance.

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1