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 (或類似的方法) 將子系新增至屬性。

適用於