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
回) 。