Bagikan melalui


XPathNavigator.Clone Metode

Definisi

Ketika ditimpa di kelas turunan, membuat posisi baru XPathNavigator pada node yang sama dengan ini 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

Mengembalikan

XPathNavigator Baru diposisikan pada node yang sama dengan ini XPathNavigator.

Contoh

Contoh berikut mendapatkan semua judul buku yang ditulis oleh Herman Melville.

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

Contoh mengambil books.xml file sebagai 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>

Keterangan

Metode Clone ini sangat berguna bersama dengan XPathNodeIterator. Digunakan XPathNodeIterator untuk mengulangi set simpul yang dipilih, dan berisi Current properti yang mengembalikan posisi XPathNavigator pada simpul XPathNodeIteratorkonteks . Namun, yang XPathNavigator dikembalikan oleh Current properti tidak dapat digunakan untuk menjauh dari set simpul. Sebagai gantinya, Anda mengkloning navigator yang dikembalikan XPathNavigator dan menggunakan navigator kloning untuk melakukan gerakan tambahan.

Kloning XPathNavigator tidak dipengaruhi oleh perubahan berikutnya ke aslinya XPathNavigator.

Berlaku untuk