XmlAttribute.CloneNode メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlAttribute.CloneNode メソッドの意味・解説 

XmlAttribute.CloneNode メソッド

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

名前空間: System.Xml
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Public Overrides Function
 CloneNode ( _
    deep As Boolean _
) As XmlNode
Dim instance As XmlAttribute
Dim deep As Boolean
Dim returnValue As XmlNode

returnValue = instance.CloneNode(deep)
public override XmlNode CloneNode (
    bool deep
)
public:
virtual XmlNode^ CloneNode (
    bool deep
) override
public XmlNode CloneNode (
    boolean deep
)
public override function CloneNode (
    deep : boolean
) : XmlNode

パラメータ

deep

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

戻り値
複製されノード

解説解説

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

指定していない属性クローン作成すると、指定した属性返されます。Specified は true返します

使用例使用例

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
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);

  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create an XmlDocument.
   XmlDocument^ doc = gcnew 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 = 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( "Display the modified XML...\r\n" );
   XmlTextWriter^ writer = gcnew XmlTextWriter( Console::Out );
   writer->Formatting = Formatting::Indented;
   doc->WriteContentTo( writer );
}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[]
 args)
    {
        //Create an XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.Load("2elems.xml");

        //Create an attribute.
        XmlAttribute attr;
        attr = doc.CreateAttribute("bk", "genre", "urn:samples");
        attr.set_Value("novel");

        //Add the attribute to the first book.
        XmlElement currNode = 
            (XmlElement)doc.get_DocumentElement().get_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.get_DocumentElement().get_LastChild();
        currNode.SetAttributeNode(attr2);

        Console.WriteLine("Display the modified XML...\r\n");
        XmlTextWriter writer = new XmlTextWriter(Console.get_Out());
        writer.set_Formatting(Formatting.Indented);
        doc.WriteContentTo(writer);
    } //main 
} //Sample

この例では、入力として、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>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlAttribute クラス
XmlAttribute メンバ
System.Xml 名前空間
XmlElement


このページでは「.NET Framework クラス ライブラリ リファレンス」からXmlAttribute.CloneNode メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からXmlAttribute.CloneNode メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からXmlAttribute.CloneNode メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlAttribute.CloneNode メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlAttribute.CloneNode メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS