ノードの複製を作成します。
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 は、常に属性ノードとそのサブツリーのクローンを作成します。クローンとして作成されたノードには属性値が含まれます。
戻り値
クローンとして作成された属性ノード。
解説
このメソッドは、ノードのコピー コンストラクタとして機能します。クローンとして作成されたノードには親がありません。 ParentNode は null 参照 (Visual Basic では Nothing) を返します。
指定していない属性のクローンを作成すると、指定した属性が返されます。 Specified は true を返します。
使用例
[Visual Basic, C#, C++] CloneNode を使用して、属性を 2 つの異なる要素のノードに追加する例を次に示します。
Imports System
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
Dim doc as XmlDocument = new XmlDocument()
doc.Load("2elems.xml")
'Create an attribute.
Dim attr as XmlAttribute
attr = doc.CreateAttribute("bk","genre","urn:samples")
attr.Value = "novel"
'Add the attribute to the first book.
Dim currNode as XmlElement
currNode = CType(doc.DocumentElement.FirstChild, XmlElement)
currNode.SetAttributeNode(attr)
'An attribute cannot be added to two different elements.
'You must clone the attribute and add it to the second book.
Dim attr2 as XmlAttribute
attr2 = CType (attr.CloneNode(true), XmlAttribute)
currNode = CType(doc.DocumentElement.LastChild, XmlElement)
currNode.SetAttributeNode(attr2)
Console.WriteLine("Display the modified XML...")
Dim writer as XmlTextWriter = new XmlTextWriter(Console.Out)
writer.Formatting = Formatting.Indented
doc.WriteContentTo(writer)
end sub
end class
[C#]
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create an XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("2elems.xml");
//Create an attribute.
XmlAttribute attr;
attr = doc.CreateAttribute("bk","genre","urn:samples");
attr.Value = "novel";
//Add the attribute to the first book.
XmlElement currNode = (XmlElement) doc.DocumentElement.FirstChild;
currNode.SetAttributeNode(attr);
//An attribute cannot be added to two different elements.
//You must clone the attribute and add it to the second book.
XmlAttribute attr2;
attr2 = (XmlAttribute) attr.CloneNode(true);
currNode = (XmlElement) doc.DocumentElement.LastChild;
currNode.SetAttributeNode(attr2);
Console.WriteLine("Display the modified XML...\r\n");
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
doc.WriteContentTo(writer);
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
//Create an XmlDocument.
XmlDocument* doc = new XmlDocument();
doc->Load(S"2elems.xml");
//Create an attribute.
XmlAttribute* attr;
attr = doc->CreateAttribute(S"bk",S"genre",S"urn:samples");
attr->Value = S"novel";
//Add the attribute to the first book.
XmlElement* currNode = dynamic_cast<XmlElement*>(doc->DocumentElement->FirstChild);
currNode->SetAttributeNode(attr);
//An attribute cannot be added to two different elements.
//You must clone the attribute and add it to the second book.
XmlAttribute* attr2;
attr2 = dynamic_cast<XmlAttribute*> (attr->CloneNode(true));
currNode = dynamic_cast<XmlElement*> (doc->DocumentElement->LastChild);
currNode->SetAttributeNode(attr2);
Console::WriteLine(S"Display the modified XML...\r\n");
XmlTextWriter* writer = new XmlTextWriter(Console::Out);
writer->Formatting = Formatting::Indented;
doc->WriteContentTo(writer);
}
この例では、入力として、 2elems.xml というファイルを使用しています。
<!--sample XML fragment-->
<bookstore>
<book ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
<book ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
<price>24.95</price>
</book>
</bookstore>
[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
参照
XmlAttribute クラス | XmlAttribute メンバ | System.Xml 名前空間 | XmlElement