次の方法で共有


XmlDocument.CloneNode メソッド

このノードの複製を作成します。

Overrides Public Function CloneNode( _
   ByVal deep As Boolean _) As XmlNode
[C#]
public override XmlNode CloneNode(booldeep);
[C++]
public: XmlNode* CloneNode(booldeep);
[JScript]
public override function CloneNode(
   deep : Boolean) : XmlNode;

パラメータ

  • deep
    指定したノードの下にあるサブツリーのクローンを順次作成していく場合は true 。指定したノードだけのクローンを作成する場合は false

戻り値

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

解説

このメソッドは、ノードのコピー コンストラクタとして機能します。クローンとして作成されたノードには親がありません。 ParentNode は null 参照 (Visual Basic では Nothing) を返します。

deep が true の場合、クローンとして作成されたノードにはすべての子ノードが含まれます。それ以外の場合は、 XmlDocument ノードだけがクローンとして作成されます。このメソッドが他のノード型でどのように動作するかを確認するには、「 XmlNode.CloneNode 」を参照してください。

使用例

[Visual Basic, C#, C++] 詳細クローンと簡易クローンの違いの例を次に示します。

 
Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample

[C#] 
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);
    
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<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(S"{0}{1}", shallow->Name, shallow->OuterXml);
    Console::WriteLine(shallow->ChildNodes->Count);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlDocument クラス | XmlDocument メンバ | System.Xml 名前空間