This documentation is archived and is not being maintained.

XmlNode.Clone Method

Creates a duplicate of this node.

[Visual Basic]
Public Overridable Function Clone() As XmlNode
public virtual XmlNode Clone();
public: virtual XmlNode* Clone();
public function Clone() : XmlNode;

Return Value

The cloned node.


Cloning an XmlElement copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes. This method recursively clones the node and the subtree underneath it.

Clone is equivalent to calling CloneNode(true).

The following table describes the specific behavior for each XmlNodeType.

XmlNodeType Clone
Attribute Clones the attribute node, including child nodes.
CData Clones the CData node, including its data content.
Comment Clones the comment node, including its text content.
Document Clones the document node, including any child nodes.
DocumentFragment Clones the document fragment node, including any child nodes.
DocumentType Clones the document type node.
Element Clones the element node, its attributes, and any child nodes.
Entity Entity nodes cannot be cloned.
EntityReference Clones the entity reference node. The replacement text is not included.
Notation Notation nodes cannot be cloned.
ProcessingInstruction Clones the processing instruction node, including its target and data.
SignificantWhitespace Clones the significant white space node, including its data value.
Text Clones the text node, including its data value.
Whitespace Clones the white space node, including its data value.
XmlDeclaration Clones the XmlDeclaration node, including its data value.
All other node types. These node types cannot be cloned.

This method is a Microsoft extension to the Document Object Model (DOM).


[Visual Basic, C#, C++] The following example clones the root node of the XML document.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "<price>19.95</price>" & _
        Dim root As XmlNode = doc.FirstChild
        'Clone the root node.  The cloned node includes
        'child nodes. This is similar to calling CloneNode(true).
        Dim clone As XmlNode = root.Clone()
    End Sub 'Main
End Class 'Sample

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +

    XmlNode root = doc.FirstChild;

    //Clone the root node.  The cloned node includes
    //child nodes. This is similar to calling CloneNode(true).
    XmlNode clone = root.Clone();

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<book ISBN='1-861001-57-5'>" 
                 S"<title>Pride And Prejudice</title>" 

    XmlNode* root = doc->FirstChild;

    //Clone the root node.  The cloned node includes
    //child nodes. This is similar to calling CloneNode(true).
    XmlNode* clone = root->Clone();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlNode Class | XmlNode Members | System.Xml Namespace | CloneNode