通过


XPathNavigator.Clone 方法

定义

在派生类中重写时,将创建一个新的 XPathNavigator 定位在与此 XPathNavigator节点相同的节点上。

public:
 abstract System::Xml::XPath::XPathNavigator ^ Clone();
public abstract System.Xml.XPath.XPathNavigator Clone();
abstract member Clone : unit -> System.Xml.XPath.XPathNavigator
Public MustOverride Function Clone () As XPathNavigator

返回

位于此XPathNavigator节点所在的同一节点的新XPathNavigator位置。

示例

以下示例获取赫曼·梅尔维尔创作的所有书名。

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

// Select all books authored by Melville.
XPathNodeIterator nodes = navigator.Select("descendant::book[author/last-name='Melville']");

while (nodes.MoveNext())
{
    // Clone the navigator returned by the Current property.
    // Use the cloned navigator to get the title element.
    XPathNavigator clone = nodes.Current.Clone();
    clone.MoveToFirstChild();
    Console.WriteLine("Book title: {0}", clone.Value);
}
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Select all books authored by Melville.
Dim nodes As XPathNodeIterator = navigator.Select("descendant::book[author/last-name='Melville']")

While nodes.MoveNext()
    ' Clone the navigator returned by the Current property. 
    ' Use the cloned navigator to get the title element.
    Dim clone As XPathNavigator = nodes.Current.Clone()
    clone.MoveToFirstChild()
    Console.WriteLine("Book title: {0}", clone.Value)
End While

该示例将 books.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>

注解

该方法 Clone 与 . XPathNodeIterator. A XPathNodeIterator 用于循环访问所选节点集,并包含一个 Current 属性,该属性返回 XPathNavigator 位于该节点的 XPathNodeIterator上下文节点上的位置。 但是, XPathNavigator 属性返回 Current 的不能用于离开节点集。 而是克隆返回 XPathNavigator 的,并使用克隆的导航器执行任何其他移动。

克隆 XPathNavigator 后不受对原始 XPathNavigator更改的影响。

适用于