XmlNamedNodeMap.SetNamedItem(XmlNode) Método

Definición

Agrega un XmlNode mediante su propiedad Name.

public:
 virtual System::Xml::XmlNode ^ SetNamedItem(System::Xml::XmlNode ^ node);
public virtual System.Xml.XmlNode SetNamedItem (System.Xml.XmlNode node);
public virtual System.Xml.XmlNode? SetNamedItem (System.Xml.XmlNode? node);
abstract member SetNamedItem : System.Xml.XmlNode -> System.Xml.XmlNode
override this.SetNamedItem : System.Xml.XmlNode -> System.Xml.XmlNode
Public Overridable Function SetNamedItem (node As XmlNode) As XmlNode

Parámetros

node
XmlNode

XmlNode que se almacenará en XmlNamedNodeMap. Si ya hay un nodo con ese nombre en el mapa, se reemplazará por el nuevo.

Devoluciones

XmlNode

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

Excepciones

node se creó a partir de un XmlDocument distinto al que creó XmlNamedNodeMap, o XmlNamedNodeMap es de sólo lectura.

Ejemplos

En el ejemplo siguiente se usa la XmlAttributeCollection clase (que hereda de XmlNamedNodeMap) para agregar un atributo a la colección.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' publicationdate='1997'> <title>Pride And Prejudice</title></book>" );
   XmlAttributeCollection^ attrColl = doc->DocumentElement->Attributes;
   
   // Add a new attribute to the collection.
   XmlAttribute^ attr = doc->CreateAttribute( "style" );
   attr->Value = "hardcover";
   attrColl->SetNamedItem( attr );
   Console::WriteLine( "Display the modified XML..." );
   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 genre='novel' publicationdate='1997'> " +
                 "  <title>Pride And Prejudice</title>" +
                 "</book>");

     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     // Add a new attribute to the collection.
     XmlAttribute attr = doc.CreateAttribute("style");
     attr.Value = "hardcover";
     attrColl.SetNamedItem(attr);

     Console.WriteLine("Display the modified XML...");
     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 genre='novel' publicationdate='1997'> " & _
                "  <title>Pride And Prejudice</title>" & _
                "</book>")
                         
    Dim attrColl as XmlAttributeCollection = doc.DocumentElement.Attributes

    ' Add a new attribute to the collection.
    Dim attr as XmlAttribute = doc.CreateAttribute("style")
    attr.Value = "hardcover"
    attrColl.SetNamedItem(attr)

    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.OuterXml)
    
  end sub
end class

Se aplica a