XPathNodeIterator 类

定义

在选定节点集上提供一个迭代器。Provides an iterator over a selected set of nodes.

public ref class XPathNodeIterator abstract : ICloneable, System::Collections::IEnumerable
public abstract class XPathNodeIterator : ICloneable, System.Collections.IEnumerable
type XPathNodeIterator = class
    interface ICloneable
    interface IEnumerable
Public MustInherit Class XPathNodeIterator
Implements ICloneable, IEnumerable
继承
XPathNodeIterator
实现

示例

下面的示例使用 XPathNavigator 类的 Select 方法,通过使用 XPathNodeIterator 类来选择节点集。The following example uses the Select method of the XPathNavigator class to select a node set using the XPathNodeIterator class.

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

该示例使用 books.xml 文件作为输入。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>  
  

注解

XPathNavigator 类返回的 XPathNodeIterator 对象未定位在选定节点集的第一个节点上。An XPathNodeIterator object returned by the XPathNavigator class is not positioned on the first node in a selected set of nodes. 必须对 XPathNodeIterator 类的 MoveNext 方法的调用,才能将 XPathNodeIterator 对象定位到选定节点集中的第一个节点上。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.

使用 XPathNodeIterator时,如果编辑当前节点或它的任何上级,则当前位置将丢失。When using the XPathNodeIterator, if you edit the current node or any of its ancestors, your current position is lost. 如果要编辑已选择的节点数,请创建 XPathNavigator 数组,将 XPathNodeIterator 中的所有节点复制到数组,然后循环访问数组并修改节点。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.

可以通过两种方法使用 XPathNodeIterator 类来循环访问 XPathNavigator 集合。There are two ways to iterate over an XPathNavigator collection by using the XPathNodeIterator class.

一种方法是使用 MoveNext 方法,然后调用 Current 以获取当前 XPathNavigator 实例,如以下示例中所示:One way is to use the MoveNext method and then call Current to get the current XPathNavigator instance, as in the following example:

while (nodeIterator->MoveNext())
{
    XPathNavigator^ n = nodeIterator->Current;
Console::WriteLine(n->LocalName);
}
while (nodeIterator.MoveNext())
{
    XPathNavigator n = nodeIterator.Current;
    Console.WriteLine(n.LocalName);
}
While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
    Console.WriteLine(n.LocalName)
End While

另一种方法是使用 foreach 循环来调用 GetEnumerator 方法,并使用返回的 IEnumerator 接口枚举节点,如以下示例中所示: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:

for each (XPathNavigator^ n in nodeIterator)
Console::WriteLine(n->LocalName);
foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);
For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next

应使用 MoveNextCurrent 或使用 GetEnumeratorYou should either use MoveNext and Current or use GetEnumerator. 组合这两种方法可能会导致意外的结果。Combining these two approaches can cause unexpected results. 例如,如果首先调用 MoveNext 方法,然后在 foreach 循环中调用 GetEnumerator 方法,则 foreach 循环不会开始从集合的开头枚举结果,而是从 Current 方法之后的位置进行枚举。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.

实施者说明

XPathNodeIterator 类继承时,必须重写以下成员:When you inherit from the XPathNodeIterator class, you must override the following members:

构造函数

XPathNodeIterator()

初始化 XPathNodeIterator 类的新实例。Initializes a new instance of the XPathNodeIterator class.

属性

Count

获取所选节点集中最后一个节点的索引。Gets the index of the last node in the selected set of nodes.

Current

在派生类中重写时,获取此 XPathNavigatorXPathNodeIterator 对象,它定位在当前上下文节点上。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.

方法

Clone()

当在派生类中重写时,返回此 XPathNodeIterator 对象的复本。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.

(继承自 Object)
GetEnumerator()

返回 IEnumerator 对象以循环访问所选的节点集。Returns an IEnumerator object to iterate through the selected node set.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MoveNext()

在派生类中重写时,将 XPathNavigator 属性返回的 Current 对象移至选定节点集中的下一个节点。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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

ICloneable.Clone()

创建作为当前实例副本的新对象。Creates a new object that is a copy of the current instance.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于