XmlAttributeCollection.SetNamedItem(XmlNode) Metodo

Definizione

Aggiunge un oggetto XmlNode utilizzando la relativa Name proprietà.

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

Parametri

node
XmlNode

Nodo dell'attributo da archiviare in questa raccolta. Il nodo sarà successivamente accessibile usando il nome del nodo. Se un nodo con tale nome è già presente nella raccolta, viene sostituito da quello nuovo; in caso contrario, il nodo viene aggiunto alla fine della raccolta.

Valori restituiti

node Se sostituisce un nodo esistente con lo stesso nome, viene restituito il nodo precedente. In caso contrario, viene restituito il nodo aggiunto.

Eccezioni

node è stato creato da un oggetto diverso XmlDocument da quello che ha creato questa raccolta.

Proprietà XmlAttributeCollection di sola lettura.

node è un oggetto XmlAttribute che è già un attributo di un altro XmlElement oggetto. Per riutilizzare gli attributi in altri elementi, è necessario clonare gli XmlAttribute oggetti da riutilizzare.

Esempio

Nell'esempio seguente viene aggiunto un nuovo attributo a un documento.

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

Si applica a