Freigeben über


XmlElement.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 XmlElement
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, um die Teilstruktur unter dem angegebenen Knoten rekursiv zu klonen. false, um nur den Knoten selbst zu klonen (und dessen Attribute, wenn der Knoten ein XmlElement ist).

Rückgabewert

Der geklonte Knoten.

Hinweise

Diese Methode wird als Kopierkonstruktor für Knoten verwendet. Für das Knotenduplikat ist kein übergeordnetes Element vorhanden (ParentNode gibt NULL (Nothing in Visual Basic) zurück).

Beispiel

Im folgenden Beispiel wird ein neues Element erstellt und geklont. Dann werden beide Elemente in ein XML-Dokument eingefügt.

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        
        Dim doc As New XmlDocument()
        doc.Load("2books.xml")
        
        ' Create a new element.
        Dim elem As XmlElement = doc.CreateElement("misc")
        elem.InnerText = "hardcover"
        elem.SetAttribute("publisher", "WorldWide Publishing")
        
        ' Clone the element so we can add one to each of the book nodes.
        Dim elem2 As XmlNode = elem.CloneNode(True)
        
        ' Add the new elements.
        doc.DocumentElement.FirstChild.AppendChild(elem)
        doc.DocumentElement.LastChild.AppendChild(elem2)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub 'Main 
End Class 'Sample
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.Load("2books.xml");

    // Create a new element.
    XmlElement elem = doc.CreateElement("misc");
    elem.InnerText = "hardcover";
    elem.SetAttribute("publisher", "WorldWide Publishing");

    // Clone the element so we can add one to each of the book nodes.
    XmlNode elem2 = elem.CloneNode(true);

    // Add the new elements.
    doc.DocumentElement.FirstChild.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(elem2);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "2books.xml" );
   
   // Create a new element.
   XmlElement^ elem = doc->CreateElement( "misc" );
   elem->InnerText = "hardcover";
   elem->SetAttribute( "publisher", "WorldWide Publishing" );
   
   // Clone the element so we can add one to each of the book nodes.
   XmlNode^ elem2 = elem->CloneNode( true );
   
   // Add the new elements.
   doc->DocumentElement->FirstChild->AppendChild( elem );
   doc->DocumentElement->LastChild->AppendChild( elem2 );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("2books.xml");

        // Create a new element.
        XmlElement elem = doc.CreateElement("misc");
        elem.set_InnerText("hardcover");
        elem.SetAttribute("publisher", "WorldWide Publishing");

        // Clone the element so we can add one to each of the book nodes.
        XmlNode elem2 = elem.CloneNode(true);

        // Add the new elements.
        doc.get_DocumentElement().get_FirstChild().AppendChild(elem);
        doc.get_DocumentElement().get_LastChild().AppendChild(elem2);
    
        Console.WriteLine("Display the modified XML...");
        doc.Save(Console.get_Out());
    } //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

XmlElement-Klasse
XmlElement-Member
System.Xml-Namespace