This documentation is archived and is not being maintained.

XmlDocument.CloneNode Method

Creates a duplicate of this node.

[Visual Basic]
Overrides Public Function CloneNode( _
   ByVal deep As Boolean _
) As XmlNode
[C#]
public override XmlNode CloneNode(
 bool deep
);
[C++]
public: XmlNode* CloneNode(
 bool deep
);
[JScript]
public override function CloneNode(
   deep : Boolean
) : XmlNode;

Parameters

deep
true to recursively clone the subtree under the specified node; false to clone only the node itself.

Return Value

The cloned XmlDocument node.

Remarks

This method serves as a copy constructor for nodes. The cloned node has no parent (ParentNode returns a null reference (Nothing in Visual Basic)).

If deep is true, the cloned node includes all the child nodes, otherwise only the XmlDocument node is cloned. See the XmlNode.CloneNode see how this method behaves on other node types.

Example

[Visual Basic, C#, C++] The following example shows the difference between a deep and shallow clone.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"  & _
                    "<title>Pride And Prejudice</title>"  & _
                    "</book>")
        
        'Create a deep clone.  The cloned node 
        'includes the child node.
        Dim deep As XmlDocument = CType(doc.CloneNode(True), XmlDocument)
        Console.WriteLine(deep.ChildNodes.Count)
        
        'Create a shallow clone.  The cloned node does not 
        'include the child node.
        Dim shallow As XmlDocument = CType(doc.CloneNode(False), XmlDocument)
        Console.WriteLine(shallow.Name + shallow.OuterXml)
        Console.WriteLine(shallow.ChildNodes.Count)
    End Sub 'Main 
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a deep clone.  The cloned node 
    //includes the child node.
    XmlDocument deep = (XmlDocument) doc.CloneNode(true);
    Console.WriteLine(deep.ChildNodes.Count);

    //Create a shallow clone.  The cloned node does not 
    //include the child node.
    XmlDocument shallow = (XmlDocument) doc.CloneNode(false);
    Console.WriteLine(shallow.Name + shallow.OuterXml);
    Console.WriteLine(shallow.ChildNodes.Count);
    
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

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

    //Create a deep clone.  The cloned node 
    //includes the child node.
    XmlDocument* deep = dynamic_cast<XmlDocument*> (doc->CloneNode(true));
    Console::WriteLine(deep->ChildNodes->Count);

    //Create a shallow clone.  The cloned node does not 
    //include the child node.
    XmlDocument* shallow = dynamic_cast<XmlDocument*> (doc->CloneNode(false));
    Console::WriteLine(S"{0}{1}", shallow->Name, shallow->OuterXml);
    Console::WriteLine(shallow->ChildNodes->Count);
}

[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.

Requirements

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

XmlDocument Class | XmlDocument Members | System.Xml Namespace

Show: