Freigeben über


XmlDocumentFragment.CloneNode-Methode

Erstellt ein Duplikat dieses Knotens.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Overrides Function CloneNode ( _
    deep As Boolean _
) As XmlNode
'Usage
Dim instance As XmlDocumentFragment
Dim deep As Boolean
Dim returnValue As XmlNode

returnValue = instance.CloneNode(deep)
public override XmlNode CloneNode (
    bool deep
)
public:
virtual XmlNode^ CloneNode (
    bool deep
) override
public XmlNode CloneNode (
    boolean deep
)
public override function CloneNode (
    deep : boolean
) : XmlNode

Parameter

  • deep
    true, wenn die Teilstruktur unter dem angegebenen Knoten rekursiv geklont werden soll, false, wenn nur der Knoten selbst geklont werden soll.

Rückgabewert

Der geklonte Knoten.

Hinweise

CloneNode wird als Kopierkonstruktor für Knoten verwendet. Informationen über das Verhalten dieser Methode bei anderen Knotentypen finden Sie unter XmlNode.CloneNode.

Für den geklonten Knoten ist kein übergeordnetes Element vorhanden (ParentNode gibt NULL (Nothing in Visual Basic) zurück).

Beispiel

Im folgenden Beispiel wird der Unterschied zwischen einem tiefen und einem flachen Klon veranschaulicht.

Imports System
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
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);    

  }
}
#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 );
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        // 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.set_InnerXml("<item>widget</item>");

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

        // Create a shallow clone.  The cloned node does
        // not include any child nodes.
        XmlNode shallow = docFrag.CloneNode(false);
        Console.WriteLine("Name: " + shallow.get_Name());
        Console.WriteLine("OuterXml: " + shallow.get_OuterXml());
    } //main 
} //Sample

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlDocumentFragment-Klasse
XmlDocumentFragment-Member
System.Xml-Namespace