XmlDocument.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.

Restituisce

XmlNode

Nodo XmlDocument clonato.

Esempio

Nell'esempio seguente viene illustrata la differenza tra un clone profondo e superficiale.

#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( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create a deep clone.  The cloned node 
   //includes the child node.
   XmlDocument^ deep = dynamic_cast<XmlDocument^>(doc->CloneNode( true ));
   Console::WriteLine( deep->ChildNodes->Count );
   
   //Create a shallow clone.  The cloned node does not 
   //include the child node.
   XmlDocument^ shallow = dynamic_cast<XmlDocument^>(doc->CloneNode( false ));
   Console::WriteLine( "{0}{1}", shallow->Name, shallow->OuterXml );
   Console::WriteLine( shallow->ChildNodes->Count );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a deep clone.  The cloned node
    //includes the child node.
    XmlDocument deep = (XmlDocument) doc.CloneNode(true);
    Console.WriteLine(deep.ChildNodes.Count);

    //Create a shallow clone.  The cloned node does not
    //include the child node.
    XmlDocument shallow = (XmlDocument) doc.CloneNode(false);
    Console.WriteLine(shallow.Name + shallow.OuterXml);
    Console.WriteLine(shallow.ChildNodes.Count);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"  & _
                    "<title>Pride And Prejudice</title>"  & _
                    "</book>")
        
        'Create a deep clone.  The cloned node 
        'includes the child node.
        Dim deep As XmlDocument = CType(doc.CloneNode(True), XmlDocument)
        Console.WriteLine(deep.ChildNodes.Count)
        
        'Create a shallow clone.  The cloned node does not 
        'include the child node.
        Dim shallow As XmlDocument = CType(doc.CloneNode(False), XmlDocument)
        Console.WriteLine(shallow.Name + shallow.OuterXml)
        Console.WriteLine(shallow.ChildNodes.Count)
    End Sub
End Class

Commenti

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

Se deep è true, il nodo clonato include tutti i nodi figlio, altrimenti solo il XmlDocument nodo viene clonato. Vedere il XmlNode.CloneNode metodo per vedere come questo metodo si comporta su altri tipi di nodo.

Si applica a