共用方式為


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。 An XPathNodeIterator 用於遍歷選取的節點集合,並包含 Current 一個性質,回傳 XPathNavigator 位於該節點上下文節點 XPathNodeIterator上的 。 然而, XPathNavigator 屬性回傳 Current 的 S 無法用來離開節點集合。 相反地,你要複製回歸 XPathNavigator 者,然後用複製的導航員來執行額外動作。

複製者 XPathNavigator 不會受到後續對原始 XPathNavigator的變更影響。

適用於