Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlNode.Clone Method

Creates a duplicate of this node.

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

public virtual XmlNode Clone ()
public XmlNode Clone ()
public function Clone () : XmlNode
Not applicable.

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

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>" +
                "</book>");

    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();
    Console.WriteLine(clone.OuterXml);
  }
}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<book ISBN='1-861001-57-5'>"
            + "<title>Pride And Prejudice</title>"
            + "<price>19.95</price>"
            + "</book>");

        XmlNode root = doc.get_FirstChild();

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft