XPathNavigator.Clone 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,請在與此 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。
範例
下列範例會取得 Herman Melville 所撰寫的所有書籍標題。
XPathDocument^ document = gcnew 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 = 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 使用。 XPathNodeIterator可用來逐一查看選取的節點集,並包含 Current 屬性,其會傳回 XPathNavigator 位於 的內容節點上 XPathNodeIterator 。 不過, XPathNavigator 屬性傳回的 Current 無法用來離開節點集。 相反地,您可以複製傳 XPathNavigator 回的 ,並使用複製的導覽器來執行任何其他移動。
XPathNavigator複製的不會受到原始 XPathNavigator 的後續變更所影響。