XPathNavigator.Clone 方法

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

**命名空间:**System.Xml.XPath
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public MustOverride Function Clone As XPathNavigator
用法
Dim instance As XPathNavigator
Dim returnValue As XPathNavigator

returnValue = instance.Clone
public abstract XPathNavigator Clone ()
public:
virtual XPathNavigator^ Clone () abstract
public abstract XPathNavigator Clone ()
public abstract function Clone () : XPathNavigator

返回值

一个新的 XPathNavigator,它定位到的节点与此 XPathNavigator 定位到的节点相同。

备注

Clone 方法在与 XPathNodeIterator 配合使用时特别有用。XPathNodeIterator 用于循环访问选定的节点集,并且它包含一个 Current 属性,该属性返回定位在 XPathNodeIterator 的上下文节点上的 XPathNavigator。但是,由 Current 属性返回的 XPathNavigator 无法用于从节点集中移开。相反,您应该克隆返回的 XPathNavigator,并使用克隆的导航器进行任何附加移动。

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

示例

下面的示例获取由 Herman Melville 创作的所有书籍的名称。

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
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);
}
XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();
XPathExpression^ query = 
    navigator->Compile("/bookstore/book");
XPathNodeIterator^ nodes = navigator->Select(query);
XPathNavigator^ nodesNavigator = nodes->Current;
for each(XPathNavigator^ navigator in nodesNavigator->
    SelectDescendants(XPathNodeType::Text, false))
{
    Console::Write(navigator->Name);
    Console::WriteLine(navigator->Value);
}

该示例采用 contosoBooks.xml 文件作为输入。

<bookstore xmlns="https://www.contoso.com/books">
    <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>

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

XPathNavigator 类
XPathNavigator 成员
System.Xml.XPath 命名空间