XmlAttributeCollection.SetNamedItem(XmlNode) 메서드

정의

해당 Name 속성을 사용하여 XmlNode를 추가합니다.

public:
 override System::Xml::XmlNode ^ SetNamedItem(System::Xml::XmlNode ^ node);
public override System.Xml.XmlNode SetNamedItem (System.Xml.XmlNode node);
public override System.Xml.XmlNode? SetNamedItem (System.Xml.XmlNode? node);
override this.SetNamedItem : System.Xml.XmlNode -> System.Xml.XmlNode
Public Overrides Function SetNamedItem (node As XmlNode) As XmlNode

매개 변수

node
XmlNode

이 컬렉션에 저장할 Attribute 노드입니다. 노드 이름을 사용하여 나중에 노드에 액세스할 수 있습니다. 컬렉션에 해당 이름의 노드가 이미 있으면 새 노드로 바뀌고, 그렇지 않으면 노드가 컬렉션 끝에 추가됩니다.

반환

XmlNode

node가 기존 노드를 같은 이름으로 바꾸면 이전 노드가 반환되고, 그렇지 않으면 추가된 노드가 반환됩니다.

예외

이 컬렉션을 만든 문서와 다른 XmlDocument에서 node를 만든 경우

XmlAttributeCollection이 읽기 전용인 경우

node가 이미 다른 XmlAttribute 개체의 특성인 XmlElement인 경우. 다른 요소에서 특성을 다시 사용하려면 다시 사용할 XmlAttribute 개체를 복제해야 합니다.

예제

다음은 문서에 새 특성을 추가하는 예제입니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create a new attribute.
   XmlAttribute^ newAttr = doc->CreateAttribute( "genre" );
   newAttr->Value = "novel";
   
   //Create an attribute collection and add the new attribute
   //to the collection.
   XmlAttributeCollection^ attrColl = doc->DocumentElement->Attributes;
   attrColl->SetNamedItem( newAttr );
   Console::WriteLine( "Display the modified XML...\r\n" );
   Console::WriteLine( doc->OuterXml );
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.SetNamedItem(newAttr);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()
  
    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")      

    'Create a new attribute.
    Dim newAttr as XmlAttribute = doc.CreateAttribute("genre")
    newAttr.Value = "novel"

    'Create an attribute collection and add the new attribute
    'to the collection.  
    Dim attrColl as XmlAttributeCollection = doc.DocumentElement.Attributes
    attrColl.SetNamedItem(newAttr)

    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.OuterXml)

  end sub
end class

적용 대상