XmlElement.SetAttributeNode メソッド

定義

新しい XmlAttribute を追加します。

オーバーロード

SetAttributeNode(XmlAttribute)

指定した XmlAttribute を追加します。

SetAttributeNode(String, String)

指定した XmlAttribute を追加します。

SetAttributeNode(XmlAttribute)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

指定した XmlAttribute を追加します。

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::Xml::XmlAttribute ^ newAttr);
public virtual System.Xml.XmlAttribute SetAttributeNode (System.Xml.XmlAttribute newAttr);
public virtual System.Xml.XmlAttribute? SetAttributeNode (System.Xml.XmlAttribute newAttr);
abstract member SetAttributeNode : System.Xml.XmlAttribute -> System.Xml.XmlAttribute
override this.SetAttributeNode : System.Xml.XmlAttribute -> System.Xml.XmlAttribute
Public Overridable Function SetAttributeNode (newAttr As XmlAttribute) As XmlAttribute

パラメーター

newAttr
XmlAttribute

この要素の属性コレクションに追加する XmlAttribute ノード。

戻り値

属性によって同じ名前の既存の属性が置換される場合は、古い XmlAttribute が返されます。それ以外の場合は、null が返されます。

例外

newAttr は、このノードを作成したドキュメントとは異なるドキュメントから作成されました。 または、このノードが読み取り専用です。

newAttr は、既に別の XmlElement オブジェクトの属性になっています。 XmlAttribute ノードのクローンを明示的に作成し、それらのクローンを他の XmlElement オブジェクトで再利用する必要があります。

注釈

その名前の属性が要素に既に存在する場合は、新しい属性に置き換えられます。

適用対象

SetAttributeNode(String, String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

指定した XmlAttribute を追加します。

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::String ^ localName, System::String ^ namespaceURI);
public virtual System.Xml.XmlAttribute SetAttributeNode (string localName, string namespaceURI);
public virtual System.Xml.XmlAttribute SetAttributeNode (string localName, string? namespaceURI);
abstract member SetAttributeNode : string * string -> System.Xml.XmlAttribute
override this.SetAttributeNode : string * string -> System.Xml.XmlAttribute
Public Overridable Function SetAttributeNode (localName As String, namespaceURI As String) As XmlAttribute

パラメーター

localName
String

属性のローカル名。

namespaceURI
String

属性の名前空間 URI。

戻り値

追加する XmlAttribute

次の例では、 属性を 要素に追加します。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   XmlElement^ root = doc->DocumentElement;
   
   // Add a new attribute.
   XmlAttribute^ attr = root->SetAttributeNode( "genre", "urn:samples" );
   attr->Value = "novel";
   Console::WriteLine( "Display the modified XML..." );
   Console::WriteLine( doc->InnerXml );
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    // Add a new attribute.
    XmlAttribute attr = root.SetAttributeNode("genre", "urn:samples");
    attr.Value="novel";

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")

    Dim root as XmlElement = doc.DocumentElement

    ' Add a new attribute.
    Dim attr as XmlAttribute = root.SetAttributeNode("genre", "urn:samples")
    attr.Value="novel"

    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.InnerXml)

  end sub
end class

注釈

には XmlAttribute 子がありません。 を使用して Value 属性にテキスト値を割り当てるか、(または同様のメソッド) を使用 AppendChild して属性に子を追加します。

適用対象