XmlDocument.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 遞迴地複製指定節點下的子樹; false 只複製節點本身。
傳回
XmlDocument複製節點。
範例
以下範例展示了深層克隆與淺層克隆的差異。
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
//Create a deep clone. The cloned node
//includes the child node.
XmlDocument deep = (XmlDocument) doc.CloneNode(true);
Console.WriteLine(deep.ChildNodes.Count);
//Create a shallow clone. The cloned node does not
//include the child node.
XmlDocument shallow = (XmlDocument) doc.CloneNode(false);
Console.WriteLine(shallow.Name + shallow.OuterXml);
Console.WriteLine(shallow.ChildNodes.Count);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
'Create the XmlDocument.
Dim doc As New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")
'Create a deep clone. The cloned node
'includes the child node.
Dim deep As XmlDocument = CType(doc.CloneNode(True), XmlDocument)
Console.WriteLine(deep.ChildNodes.Count)
'Create a shallow clone. The cloned node does not
'include the child node.
Dim shallow As XmlDocument = CType(doc.CloneNode(False), XmlDocument)
Console.WriteLine(shallow.Name + shallow.OuterXml)
Console.WriteLine(shallow.ChildNodes.Count)
End Sub
End Class
備註
此方法作為節點的複製建構子。 被克隆的節點沒有父節點(ParentNode 回傳 null)。
若 deep 為 true,則克隆節點包含所有子節點,否則僅克隆該 XmlDocument 節點。 請參閱該 XmlNode.CloneNode 方法,了解此方法在其他節點類型的表現。