XmlElement.SetAttributeNode 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
添加一个新 XmlAttribute。
重载
| SetAttributeNode(XmlAttribute) | 
						 添加指定的 XmlAttribute。  | 
        	
| SetAttributeNode(String, String) | 
						 添加指定的 XmlAttribute。  | 
        	
SetAttributeNode(XmlAttribute)
- Source:
 - XmlElement.cs
 
- 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
 
- 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;
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 (或类似方法) 向属性添加子级。