XmlAttributeCollection.SetNamedItem(XmlNode) メソッド

定義

XmlNode プロパティを使用してNameを追加します。

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

パラメーター

node
XmlNode

このコレクションに格納する属性ノード。 ノードは、後でノードの名前を使用してアクセスできるようになります。 その名前のノードがコレクションに既に存在する場合は、新しいノードに置き換えられます。それ以外の場合、ノードはコレクションの末尾に追加されます。

返品

nodeが既存のノードを同じ名前に置き換えた場合、古いノードが返されます。それ以外の場合は、追加されたノードが返されます。

例外

node は、このコレクションを作成した XmlDocument とは異なるから作成されました。

この XmlAttributeCollection は読み取り専用です。

nodeは、既に別のXmlElement オブジェクトの属性であるXmlAttributeです。 他の要素で属性を再利用するには、再利用する XmlAttribute オブジェクトを複製する必要があります。

次の例では、ドキュメントに新しい属性を追加します。

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

適用対象