XmlAttributeCollection.SetNamedItem(XmlNode) Método

Definição

Adiciona um XmlNode uso de sua Name propriedade.

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

Um nó de atributo a ser armazenado nesta coleção. Posteriormente, o nó estará acessível usando o nome do nó. Se um nó com esse nome já estiver presente na coleção, ele será substituído pelo novo; caso contrário, o nó será acrescentado ao final da coleção.

Retornos

Se o node nó existente substituir um nó existente pelo mesmo nome, o nó antigo será retornado; caso contrário, o nó adicionado será retornado.

Exceções

node foi criado a partir de um diferente XmlDocument daquele que criou essa coleção.

Isso XmlAttributeCollection é somente leitura.

node é um XmlAttribute atributo que já é um atributo de outro XmlElement objeto. Para reutilize atributos em outros elementos, você deve clonar os XmlAttribute objetos que deseja reutilize.

Exemplos

O exemplo a seguir adiciona um novo atributo a um 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

Aplica-se a