XmlAttributeCollection.SetNamedItem(XmlNode) Método

Definición

Agrega un objeto XmlNode mediante su propiedad Name.

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 almacenará en la colección. Posteriormente se podrá obtener acceso al nodo utilizando el nombre del nodo en cuestión. Si ya hay un nodo con ese nombre en la colección, se reemplazará por el nuevo; en caso contrario, el nodo se agregará al final de la colección.

Devoluciones

XmlNode

Si node reemplaza a un nodo existente con el mismo nombre, se devolverá el nodo antiguo; en caso contrario, se devolverá el nodo agregado.

Excepciones

Se creó node a partir de un XmlDocument distinto al que creó esta colección.

Esta XmlAttributeCollection es de sólo lectura.

node es un XmlAttribute que, a su vez, es un atributo de otro objeto XmlElement. Para volver a utilizar atributos en otros elementos, hay que duplicar los objetos XmlAttribute que se deseen volver a utilizar.

Ejemplos

En el ejemplo siguiente se agrega un nuevo atributo a un documento.

#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

Se aplica a