XmlAttribute.CloneNode(Boolean) 메서드

정의

이 노드의 복제본을 만듭니다.

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

지정된 노드 아래의 하위 트리를 재귀적으로 복제하려면 true이고, 노드 자체만 복제하려면 false입니다.

반환

복제 노드입니다.

예제

다음 예제에서는 를 사용하여 CloneNode 두 개의 서로 다른 요소 노드에 특성을 추가합니다.

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

이 예제에서는 파일 를 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>

설명

이 메서드는 노드에 대한 복사 생성자 역할을 합니다. 복제된 노드에는 부모가 없습니다(ParentNode 반환 null).

지정되지 않은 특성을 복제하면 지정된 특성(가 반환됨)Specified 이 반환 true됩니다.

적용 대상

추가 정보