XmlAttributeCollection.SetNamedItem(XmlNode) Metoda

Definicja

Dodaje obiekt XmlNode używający jego Name właściwości.

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

Parametry

node
XmlNode

Węzeł atrybutu do przechowywania w tej kolekcji. Węzeł będzie później dostępny przy użyciu nazwy węzła. Jeśli węzeł o tej nazwie jest już obecny w kolekcji, zostanie zastąpiony przez nowy; w przeciwnym razie węzeł jest dołączany na końcu kolekcji.

Zwraca

XmlNode

node Jeśli istniejący węzeł zostanie zastąpiony tą samą nazwą, zostanie zwrócony stary węzeł. W przeciwnym razie zostanie zwrócony dodany węzeł.

Wyjątki

node został utworzony na podstawie innej XmlDocument kolekcji niż ta, która utworzyła tę kolekcję.

Jest to XmlAttributeCollection tylko do odczytu.

nodeXmlAttribute jest to już atrybut innego XmlElement obiektu. Aby ponownie używać atrybutów w innych elementach, należy sklonować XmlAttribute obiekty, których chcesz ponownie użyć.

Przykłady

Poniższy przykład dodaje nowy atrybut do dokumentu.

#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

Dotyczy