XmlElement.CloneNode(Boolean) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a duplicate of this node.
public:
override System::Xml::XmlNode ^ CloneNode(bool deep);
public override System.Xml.XmlNode CloneNode (bool deep);
override this.CloneNode : bool -> System.Xml.XmlNode
Public Overrides Function CloneNode (deep As Boolean) As XmlNode
Parameters
- deep
- 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
).
Returns
The cloned node.
Examples
The following example creates a new element, clones it, and then adds both elements into an XML document.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlDocument^ doc = gcnew 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 );
}
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);
}
}
Option Explicit
Option Strict
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
End Class
Remarks
This method serves as a copy constructor for nodes. The duplicate node has no parent (ParentNode returns null
).
Applies to
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기