XmlElement.CloneNode Method (Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a duplicate of this node.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public override XmlNode CloneNode(
	bool deep


Type: System.Boolean

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

Type: System.Xml.XmlNode

The cloned node.

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

The following example creates a new element, clones it, and then adds both elements into an XML document.

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

public class Sample
  public static void Main()

    XmlDocument doc = new XmlDocument();

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

    Console.WriteLine("Display the modified XML...");


Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top