XmlDocumentFragment.CloneNode(Boolean) 方法

定义

创建此节点的副本。

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

返回

克隆的节点。

示例

以下示例演示深层克隆和浅表克隆之间的差异。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<items/>" );
   
   // Create a document fragment.
   XmlDocumentFragment^ docFrag = doc->CreateDocumentFragment();
   
   // Set the contents of the document fragment.
   docFrag->InnerXml = "<item>widget</item>";
   
   // Create a deep clone.  The cloned node
   // includes child nodes.
   XmlNode^ deep = docFrag->CloneNode( true );
   Console::WriteLine( "Name: {0}", deep->Name );
   Console::WriteLine( "OuterXml: {0}", deep->OuterXml );
   
   // Create a shallow clone.  The cloned node does
   // not include any child nodes.
   XmlNode^ shallow = docFrag->CloneNode( false );
   Console::WriteLine( "Name: {0}", shallow->Name );
   Console::WriteLine( "OuterXml: {0}", shallow->OuterXml );
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<items/>");

    // Create a document fragment.
    XmlDocumentFragment docFrag = doc.CreateDocumentFragment();

    // Set the contents of the document fragment.
    docFrag.InnerXml ="<item>widget</item>";

    // Create a deep clone.  The cloned node
    // includes child nodes.
    XmlNode deep = docFrag.CloneNode(true);
    Console.WriteLine("Name: " + deep.Name);
    Console.WriteLine("OuterXml: " + deep.OuterXml);

    // Create a shallow clone.  The cloned node does
    // not include any child nodes.
    XmlNode shallow = docFrag.CloneNode(false);
    Console.WriteLine("Name: " + shallow.Name);
    Console.WriteLine("OuterXml: " + shallow.OuterXml);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()
    
    ' Create the XmlDocument.
    Dim doc as XmlDocument  = new XmlDocument()
    doc.LoadXml("<items/>")

    ' Create a document fragment.
    Dim docFrag as XmlDocumentFragment = doc.CreateDocumentFragment()

    ' Set the contents of the document fragment.
    docFrag.InnerXml ="<item>widget</item>"

    ' Create a deep clone.  The cloned node
    ' includes child nodes.
    Dim deep as XmlNode = docFrag.CloneNode(true)
    Console.WriteLine("Name: " + deep.Name)
    Console.WriteLine("OuterXml: " + deep.OuterXml)

    ' Create a shallow clone.  The cloned node does
    ' not include any child nodes.
    Dim shallow as XmlNode = docFrag.CloneNode(false)
    Console.WriteLine("Name: " + shallow.Name)
    Console.WriteLine("OuterXml: " + shallow.OuterXml)    

  end sub
end class

注解

CloneNode 用作节点的复制构造函数。 若要查看此方法与其他节点类型的行为方式,请参阅 XmlNode.CloneNode

克隆的节点没有父 (ParentNode 返回 null) 。

适用于