CloneNode Method

XmlElement.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 (and its attributes if the node is an XmlElement).

Return Value

The cloned node.

Remarks

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

Example

[Visual Basic, C#, C++] The following example creates a new element, clones it, and then adds both elements into an 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.Load("2books.xml")
        
        ' Create a new element.
        Dim elem As XmlElement = doc.CreateElement("misc")
        elem.InnerText = "hardcover"
        elem.SetAttribute("publisher", "WorldWide Publishing")
        
        ' Clone the element so we can add one to each of the book nodes.
        Dim elem2 As XmlNode = elem.CloneNode(True)
        
        ' Add the new elements.
        doc.DocumentElement.FirstChild.AppendChild(elem)
        doc.DocumentElement.LastChild.AppendChild(elem2)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub 'Main 
End Class 'Sample

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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.Load("2books.xml");

    // Create a new element.
    XmlElement elem = doc.CreateElement("misc");
    elem.InnerText = "hardcover";
    elem.SetAttribute("publisher", "WorldWide Publishing");

    // Clone the element so we can add one to each of the book nodes.
    XmlNode elem2 = elem.CloneNode(true);

    // Add the new elements.
    doc.DocumentElement.FirstChild.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(elem2);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

[C++] 
#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->Load(S"2books.xml");

    // Create a new element.
    XmlElement* elem = doc->CreateElement(S"misc");
    elem->InnerText = S"hardcover";
    elem->SetAttribute(S"publisher", S"WorldWide Publishing");

    // Clone the element so we can add one to each of the book nodes.
    XmlNode* elem2 = elem->CloneNode(true);

    // Add the new elements.
    doc->DocumentElement->FirstChild->AppendChild(elem);
    doc->DocumentElement->LastChild->AppendChild(elem2);

    Console::WriteLine(S"Display the modified XML...");
    doc->Save(Console::Out);
}

[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

XmlElement Class | XmlElement Members | System.Xml Namespace

Show:
© 2016 Microsoft