Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlNode.Clone Method

Creates a duplicate of this node.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public virtual XmlNode Clone()

Return Value

Type: System.Xml.XmlNode
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.




Clones the attribute node, including child nodes.


Clones the CData node, including its data content.


Clones the comment node, including its text content.


Clones the document node, including any child nodes.


Clones the document fragment node, including any child nodes.


Clones the document type node.


Clones the element node, its attributes, and any child nodes.


Entity nodes cannot be cloned.


Clones the entity reference node. The replacement text is not included.


Notation nodes cannot be cloned.


Clones the processing instruction node, including its target and data.


Clones the significant white space node, including its data value.


Clones the text node, including its data value.


Clones the white space node, including its data value.


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

The following example clones the root node of the XML document.

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();

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.