XmlDocument.CloneNode(Boolean) XmlDocument.CloneNode(Boolean) XmlDocument.CloneNode(Boolean) XmlDocument.CloneNode(Boolean) Method

定義

このノードの複製を作成します。Creates a duplicate of this node.

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 Boolean Boolean Boolean

指定したノードの下にあるサブツリーのクローンを順次作成していく場合は true。指定したノードだけのクローンを作成する場合は falsetrue to recursively clone the subtree under the specified node; false to clone only the node itself.

戻り値

クローンとして作成された XmlDocument ノード。The cloned XmlDocument node.

次の例は、ディープクローンと浅い複製の違いを示しています。The following example shows the difference between a deep and shallow clone.

#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 'Sample

注釈

このメソッドは、ノードのコピーコンストラクターとして機能します。This method serves as a copy constructor for nodes. 複製されたノードにはParentNodenullがありません (を返します)。The cloned node has no parent (ParentNode returns null).

deep XmlDocumentの場合、複製されたノードにはすべての子ノードが含まれます。それ以外の場合は、ノードだけが複製されます。 trueIf deep is true, the cloned node includes all the child nodes, otherwise only the XmlDocument node is cloned. このメソッドXmlNode.CloneNodeが他のノード型でどのように動作するかを確認するには、メソッドを参照してください。See the XmlNode.CloneNode method to see how this method behaves on other node types.

適用対象