XmlAttributeCollection.SetNamedItem(XmlNode) Méthode

Définition

Ajoute une XmlNode utilisation de sa Name propriété.

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

Paramètres

node
XmlNode

Nœud d’attribut à stocker dans cette collection. Le nœud sera plus tard accessible à l’aide du nom du nœud. Si un nœud portant ce nom est déjà présent dans la collection, il est remplacé par le nouveau nœud ; sinon, le nœud est ajouté à la fin de la collection.

Retours

Si le node nœud remplace un nœud existant portant le même nom, l’ancien nœud est retourné ; sinon, le nœud ajouté est retourné.

Exceptions

node a été créé à partir d’un autre XmlDocument que celui qui a créé cette collection.

Il s’agit XmlAttributeCollection d’une lecture seule.

node est un XmlAttribute attribut d’un autre XmlElement objet. Pour réutiliser des attributs dans d’autres éléments, vous devez cloner les XmlAttribute objets que vous souhaitez réutiliser.

Exemples

L’exemple suivant ajoute un nouvel attribut à un document.

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

S’applique à