次の方法で共有


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

戻り値

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

解説

CloneNode は、ノードのコピー コンストラクタとして機能します。このメソッドが他のノードの種類ではどのように動作するかを確認するには、 XmlNode.CloneNode のトピックを参照してください。

クローンとして作成されたノードには親がありません。 ParentNode は null 参照 (Visual Basic では Nothing) を返します。

使用例

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

 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()
    
    ' Create the XmlDocument.
    Dim doc as XmlDocument  = new XmlDocument()
    doc.LoadXml("<items/>")

    ' Create a document fragment.
    Dim docFrag as XmlDocumentFragment = doc.CreateDocumentFragment()

    ' Set the contents of the document fragment.
    docFrag.InnerXml ="<item>widget</item>"

    ' Create a deep clone.  The cloned node
    ' includes child nodes.
    Dim deep as XmlNode = docFrag.CloneNode(true)
    Console.WriteLine("Name: " + deep.Name)
    Console.WriteLine("OuterXml: " + deep.OuterXml)

    ' Create a shallow clone.  The cloned node does
    ' not include any child nodes.
    Dim shallow as XmlNode = docFrag.CloneNode(false)
    Console.WriteLine("Name: " + shallow.Name)
    Console.WriteLine("OuterXml: " + shallow.OuterXml)    

  end sub
end class

[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("<items/>");

    // Create a document fragment.
    XmlDocumentFragment docFrag = doc.CreateDocumentFragment();

    // Set the contents of the document fragment.
    docFrag.InnerXml ="<item>widget</item>";

    // Create a deep clone.  The cloned node
    // includes child nodes.
    XmlNode deep = docFrag.CloneNode(true);
    Console.WriteLine("Name: " + deep.Name);
    Console.WriteLine("OuterXml: " + deep.OuterXml);

    // Create a shallow clone.  The cloned node does
    // not include any child nodes.
    XmlNode shallow = docFrag.CloneNode(false);
    Console.WriteLine("Name: " + shallow.Name);
    Console.WriteLine("OuterXml: " + shallow.OuterXml);    

  }
}

[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"<items/>");

     // Create a document fragment.
     XmlDocumentFragment* docFrag = doc->CreateDocumentFragment();
 
     // Set the contents of the document fragment.
     docFrag->InnerXml =S"<item>widget</item>";
 
     // Create a deep clone.  The cloned node
     // includes child nodes.
     XmlNode* deep = docFrag->CloneNode(true);
     Console::WriteLine(S"Name: {0}", deep->Name);
     Console::WriteLine(S"OuterXml: {0}", deep->OuterXml);

     // Create a shallow clone.  The cloned node does
     // not include any child nodes.
     XmlNode* shallow = docFrag->CloneNode(false);
     Console::WriteLine(S"Name: {0}", shallow->Name);
     Console::WriteLine(S"OuterXml: {0}", shallow->OuterXml);    
}

[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

参照

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