XmlNode.Clone XmlNode.Clone XmlNode.Clone XmlNode.Clone Method


建立這個節點的複本。Creates a duplicate of this node.

 virtual System::Xml::XmlNode ^ Clone();
public virtual System.Xml.XmlNode Clone ();
abstract member Clone : unit -> System.Xml.XmlNode
override this.Clone : unit -> System.Xml.XmlNode
Public Overridable Function Clone () As XmlNode


複製的節點。The cloned node.


下列範例會複製 XML 檔的根節點。The following example clones the root node of the XML document.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book ISBN='1-861001-57-5'>"
   "<title>Pride And Prejudice</title>"
   "</book>" );
   XmlNode^ root = doc->FirstChild;
   //Clone the root node.  The cloned node includes
   //child nodes. This is similar to calling CloneNode(true).
   XmlNode^ clone = root->Clone();
   Console::WriteLine( clone->OuterXml );

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +

    XmlNode root = doc.FirstChild;

    //Clone the root node.  The cloned node includes
    //child nodes. This is similar to calling CloneNode(true).
    XmlNode clone = root.Clone();
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "<price>19.95</price>" & _
        Dim root As XmlNode = doc.FirstChild
        'Clone the root node.  The cloned node includes
        'child nodes. This is similar to calling CloneNode(true).
        Dim clone As XmlNode = root.Clone()
    End Sub
End Class 'Sample


XmlElement複製所有屬性及其值,包括由 XML 處理器產生以代表預設屬性的屬性。Cloning an XmlElement copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes. 這個方法會以遞迴方式複製節點及其底下的子樹。This method recursively clones the node and the subtree underneath it.

Clone相當於呼叫CloneNode(true)Clone is equivalent to calling CloneNode(true).

下表描述每個XmlNodeType的特定行為。The following table describes the specific behavior for each XmlNodeType.

XmlNodeTypeXmlNodeType 複製Clone
屬性Attribute 複製屬性節點,包括子節點。Clones the attribute node, including child nodes.
CDataCData 複製 CData 節點,包括其資料內容。Clones the CData node, including its data content.
註解Comment 複製批註節點,包括其文字內容。Clones the comment node, including its text content.
文件Document 複製檔節點,包括任何子節點。Clones the document node, including any child nodes.
DocumentFragmentDocumentFragment 複製檔片段節點,包括任何子節點。Clones the document fragment node, including any child nodes.
DocumentTypeDocumentType 複製檔案類型節點。Clones the document type node.
元素Element 複製元素節點、其屬性和任何子節點。Clones the element node, its attributes, and any child nodes.
實體Entity 無法複製 Entity 節點。Entity nodes cannot be cloned.
EntityReferenceEntityReference 複製實體參考節點。Clones the entity reference node. 不包括取代文字。The replacement text is not included.
NotationNotation 標記法節點無法複製。Notation nodes cannot be cloned.
ProcessingInstructionProcessingInstruction 複製處理指示節點,包括其目標和資料。Clones the processing instruction node, including its target and data.
SignificantWhitespaceSignificantWhitespace 複製重要的空白字元節點,包括其資料值。Clones the significant white space node, including its data value.
TextText 複製文位元組點,包括其資料值。Clones the text node, including its data value.
WhitespaceWhitespace 複製泛空白字元節點,包括其資料值。Clones the white space node, including its data value.
XmlDeclarationXmlDeclaration 複製 XmlDeclaration 節點,包括其資料值。Clones the XmlDeclaration node, including its data value.
所有其他節點類型。All other node types. 無法複製這些節點類型。These node types cannot be cloned.

這個方法是檔物件模型(DOM)的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).