Share via


XmlElement.CloneNode(Boolean) Metodo

Definizione

Crea un duplicato del nodo.

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

Parametri

deep
Boolean

true per clonare in modo ricorsivo il sottoalbero del nodo specificato, false per clonare solo il nodo (e gli attributi se il nodo è un XmlElement).

Restituisce

XmlNode

Nodo clonato.

Esempio

L'esempio seguente crea un nuovo elemento, lo clona e quindi aggiunge entrambi gli elementi in un documento XML.

#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 );
}
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);
  }
}
Option Explicit
Option Strict

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
End Class

Commenti

Questo metodo funge da costruttore di copia per i nodi. Il nodo duplicato non ha alcun elemento padre (ParentNode restituisce null).

Si applica a