共用方式為


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

複製的節點沒有父節點 (ParentNodenull 回) 。

適用於