XmlElement.CloneNode(Boolean) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建此节点的副本。
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
参数
- deep
- Boolean
若要递归地克隆指定节点下的子树,则为 true
;若仅克隆节点本身(如果节点是 XmlElement
,还克隆其属性),则为 false
。
返回
克隆的节点。
示例
以下示例创建一个新元素,对其进行克隆,然后将这两个元素添加到 XML 文档中。
#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
注解
此方法充当节点的复制构造函数。 重复节点没有父 (ParentNode 返回 null
) 。