XmlAttributeCollection.SetNamedItem(XmlNode) Metode

Definisi

XmlNode Menambahkan menggunakan propertinyaName.

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

Parameter

node
XmlNode

Simpul atribut untuk disimpan dalam koleksi ini. Simpul nantinya akan dapat diakses menggunakan nama simpul. Jika simpul dengan nama tersebut sudah ada dalam koleksi, simpul tersebut digantikan oleh yang baru; jika tidak, simpul ditambahkan ke akhir koleksi.

Mengembalikan

node Jika mengganti simpul yang ada dengan nama yang sama, simpul lama dikembalikan; jika tidak, simpul yang ditambahkan dikembalikan.

Pengecualian

node dibuat dari yang berbeda XmlDocument dari yang membuat koleksi ini.

Ini XmlAttributeCollection baca-saja.

nodeXmlAttribute adalah yang sudah merupakan atribut dari objek lainXmlElement. Untuk menggunakan kembali atribut di elemen lain, Anda harus mengkloning objek yang XmlAttribute ingin Anda gunakan kembali.

Contoh

Contoh berikut menambahkan atribut baru ke dokumen.

#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

Berlaku untuk