XmlDocumentFragment::CloneNode Method (Boolean)

 

Creates a duplicate of this node.

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

public:
virtual XmlNode^ CloneNode(
	bool deep
) override

Parameters

deep
Type: System::Boolean

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

Return Value

Type: System.Xml::XmlNode^

The cloned node.

CloneNode serves as a copy constructor for nodes. To see how this method behaves with other node types, see XmlNode::CloneNode.

The cloned node has no parent (ParentNode returns null).

The following example shows the difference between a deep and shallow clone.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{

   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<items/>" );

   // Create a document fragment.
   XmlDocumentFragment^ docFrag = doc->CreateDocumentFragment();

   // Set the contents of the document fragment.
   docFrag->InnerXml = "<item>widget</item>";

   // Create a deep clone.  The cloned node
   // includes child nodes.
   XmlNode^ deep = docFrag->CloneNode( true );
   Console::WriteLine( "Name: {0}", deep->Name );
   Console::WriteLine( "OuterXml: {0}", deep->OuterXml );

   // Create a shallow clone.  The cloned node does
   // not include any child nodes.
   XmlNode^ shallow = docFrag->CloneNode( false );
   Console::WriteLine( "Name: {0}", shallow->Name );
   Console::WriteLine( "OuterXml: {0}", shallow->OuterXml );
}

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