XmlNode.Clone 方法

定义

创建此节点的副本。Creates a duplicate of this node.

public:
 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>"
   "<price>19.95</price>"
   "</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>" +
                "</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);
  }
}
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>" & _
                    "</book>")
        
        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()
        Console.WriteLine(clone.OuterXml)
    End Sub
End Class

注解

克隆 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.
DocumentDocument 克隆文档节点,包括任何子节点。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).

适用于

另请参阅