XmlAttributeCollection.SetNamedItem(XmlNode) Método

Definición

Agrega un XmlNode objeto mediante su Name propiedad .

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

Parámetros

node
XmlNode

Nodo de atributo que se va a almacenar en esta colección. Más adelante se podrá acceder al nodo con el nombre del nodo. Si un nodo con ese nombre ya está presente en la colección, se reemplaza por el nuevo; de lo contrario, el nodo se anexa al final de la colección.

Devoluciones

node Si reemplaza un nodo existente con el mismo nombre, se devuelve el nodo anterior; de lo contrario, se devuelve el nodo agregado.

Excepciones

node se creó a partir de una diferente XmlDocument de la que creó esta colección.

Esto XmlAttributeCollection es de solo lectura.

node es un XmlAttribute que ya es un atributo de otro XmlElement objeto. Para volver a usar atributos en otros elementos, debe clonar los XmlAttribute objetos que desea volver a usar.

Ejemplos

En el ejemplo siguiente se agrega un nuevo atributo 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

Se aplica a