This documentation is archived and is not being maintained.

XPathNavigator.Clone Method

When overridden in a derived class, creates a new XPathNavigator positioned at the same node as this XPathNavigator.

Namespace: System.Xml.XPath
Assembly: System.Xml (in system.xml.dll)

public abstract XPathNavigator Clone ()
public abstract XPathNavigator Clone ()
public abstract function Clone () : XPathNavigator

Return Value

A new XPathNavigator positioned at the same node as this XPathNavigator.

The Clone method is especially useful in conjunction with the XPathNodeIterator. An XPathNodeIterator is used to iterate over a selected node set, and contains a Current property which returns an XPathNavigator positioned on the context node of the XPathNodeIterator. However, the XPathNavigator returned by the Current property cannot be used to move away from the node set. Instead, you clone the returned XPathNavigator and use the cloned navigator to do any additional moves.

The cloned XPathNavigator is not affected by subsequent changes to the original XPathNavigator.

The following example gets all book titles authored by 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();
    Console.WriteLine("Book title: {0}", clone.Value);

The example takes the contosoBooks.xml file as an input.

<bookstore xmlns="">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0