XmlElement.SetAttributeNode 方法

定义

添加一个新 XmlAttributeAdds a new XmlAttribute.

重载

SetAttributeNode(XmlAttribute)

添加指定的 XmlAttributeAdds the specified XmlAttribute.

SetAttributeNode(String, String)

添加指定的 XmlAttributeAdds the specified 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);
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 节点。The XmlAttribute node to add to the attribute collection for this element.

返回

XmlAttribute

如果该特性替换同名现有特性,则返回旧 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)

添加指定的 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);
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

属性的本地名称。The local name of the attribute.

namespaceURI
String

属性的命名空间 URI。The namespace URI of the attribute.

返回

XmlAttribute

要添加的 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.

适用于