XmlElement.SetAttributeNode XmlElement.SetAttributeNode XmlElement.SetAttributeNode XmlElement.SetAttributeNode Method

定義

加入新的 XmlAttributeAdds a new XmlAttribute.

多載

SetAttributeNode(XmlAttribute) SetAttributeNode(XmlAttribute) SetAttributeNode(XmlAttribute) SetAttributeNode(XmlAttribute)

加入指定的 XmlAttributeAdds the specified XmlAttribute.

SetAttributeNode(String, String) SetAttributeNode(String, String) SetAttributeNode(String, String) SetAttributeNode(String, String)

加入指定的 XmlAttributeAdds the specified XmlAttribute.

SetAttributeNode(XmlAttribute) SetAttributeNode(XmlAttribute) SetAttributeNode(XmlAttribute) SetAttributeNode(XmlAttribute)

加入指定的 XmlAttributeAdds the specified XmlAttribute.

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 XmlAttribute

要加入這個項目屬性集合中的 XmlAttribute 節點。The XmlAttribute node to add to the attribute collection for this element.

傳回

如果這個屬性取代相同名稱的現有屬性,會傳回舊的 XmlAttribute;否則會傳回 nullIf the attribute replaces an existing attribute with the same name, the old XmlAttribute is returned; otherwise, null is returned.

例外狀況

newAttr 由不同於建立這個節點的另一份文件所建立。The newAttr was created from a different document than the one that created this node. 或者這個節點是唯讀的。Or this node is read-only.

newAttr 已經是其他 XmlElement 物件的屬性。The newAttr is already an attribute of another XmlElement object. 您必須明確複製 XmlAttribute 節點,以便在其他 XmlElement 物件中重複使用這些節點。You must explicitly clone XmlAttribute nodes to re-use them in other XmlElement objects.

備註

如果專案中已有同名的屬性, 則會將它取代為新的。If an attribute with that name is already present in the element, it is replaced by the new one.

SetAttributeNode(String, String) SetAttributeNode(String, String) SetAttributeNode(String, String) SetAttributeNode(String, String)

加入指定的 XmlAttributeAdds the specified XmlAttribute.

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::String ^ localName, System::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 String String String

屬性的本機名稱。The local name of the attribute.

namespaceURI
String String String String

屬性的命名空間 URI。The namespace URI of the attribute.

傳回

要加入的 XmlAttributeThe XmlAttribute to add.

範例

下列範例會將屬性新增至元素。The following example adds an attribute to an element.

#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沒有任何子系。The XmlAttribute does not have any children. 使用Value將文字值指派給屬性, 或使用AppendChild (或類似的方法) 將子系加入至屬性。Use Value to assign a text value to the attribute or use AppendChild (or a similar method) to add children to the attribute.

適用於