XmlDocument.CreateAttribute 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建具有指定名称的 XmlAttribute。
重载
| CreateAttribute(String) | 创建具有指定 Name 的 XmlAttribute。 | 
| CreateAttribute(String, String) | 创建具有指定限定名和 NamespaceURI 的 XmlAttribute。 | 
| CreateAttribute(String, String, String) | 创建一个具有指定的 Prefix、LocalName 和 NamespaceURI 的 XmlAttribute。 | 
CreateAttribute(String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建具有指定 Name 的 XmlAttribute。
public:
 System::Xml::XmlAttribute ^ CreateAttribute(System::String ^ name);public System.Xml.XmlAttribute CreateAttribute (string name);member this.CreateAttribute : string -> System.Xml.XmlAttributePublic Function CreateAttribute (name As String) As XmlAttribute参数
- name
- String
属性的限定名称。 如果名称包含冒号,则 Prefix 属性反映名称中第一个冒号之前的部分,LocalName 属性反映名称中第一个冒号之后的部分。 
              NamespaceURI 保持为空,除非该前缀是一个可识别的内置前缀,例如 xmlns。 在这种情况下 NamespaceURI 的值为 http://www.w3.org/2000/xmlns/。
返回
新的 XmlAttribute。
示例
下面创建一个属性,并将其添加到 XML 文档。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create an attribute.
   XmlAttribute^ attr = doc->CreateAttribute( "publisher" );
   attr->Value = "WorldWide Publishing";
   
   //Add the new node to the document. 
   doc->DocumentElement->SetAttributeNode( attr );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");
    //Create an attribute.
    XmlAttribute attr = doc.CreateAttribute("publisher");
    attr.Value = "WorldWide Publishing";
    //Add the new node to the document.
    doc.DocumentElement.SetAttributeNode(attr);
    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml
Public Class Sample
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"  & _
                    "<title>Pride And Prejudice</title>"  & _
                    "</book>")
        
        'Create an attribute.
        Dim attr As XmlAttribute = doc.CreateAttribute("publisher")
        attr.Value = "WorldWide Publishing"
        
        'Add the new node to the document. 
        doc.DocumentElement.SetAttributeNode(attr)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class
注解
              XmlAttribute可以使用 方法将 添加到 。XmlElementSetAttributeNode
适用于
CreateAttribute(String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建具有指定限定名和 NamespaceURI 的 XmlAttribute。
public:
 System::Xml::XmlAttribute ^ CreateAttribute(System::String ^ qualifiedName, System::String ^ namespaceURI);public System.Xml.XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI);public System.Xml.XmlAttribute CreateAttribute (string qualifiedName, string? namespaceURI);member this.CreateAttribute : string * string -> System.Xml.XmlAttributePublic Function CreateAttribute (qualifiedName As String, namespaceURI As String) As XmlAttribute参数
- namespaceURI
- String
属性的 namespaceURI。 如果限定名称包含前缀 xmlns,则该参数必须是 http://www.w3.org/2000/xmlns/。
返回
新的 XmlAttribute。
注解
              XmlAttribute可以使用 方法将 添加到 。XmlElementSetAttributeNode
适用于
CreateAttribute(String, String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建一个具有指定的 Prefix、LocalName 和 NamespaceURI 的 XmlAttribute。
public:
 virtual System::Xml::XmlAttribute ^ CreateAttribute(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI);public virtual System.Xml.XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI);public virtual System.Xml.XmlAttribute CreateAttribute (string? prefix, string localName, string? namespaceURI);abstract member CreateAttribute : string * string * string -> System.Xml.XmlAttribute
override this.CreateAttribute : string * string * string -> System.Xml.XmlAttributePublic Overridable Function CreateAttribute (prefix As String, localName As String, namespaceURI As String) As XmlAttribute参数
- prefix
- String
属性的前缀(如果有的话)。 String.Empty 与 null 等效。
- localName
- String
属性的本地名称。
- namespaceURI
- String
属性的命名空间 URI(如果有的话)。 String.Empty 与 null 等效。 如果 prefix 为 xmlns,则此参数必须是 http://www.w3.org/2000/xmlns/;否则将引发异常。
返回
新的 XmlAttribute。
注解
              XmlAttribute可以使用 方法将 添加到 。XmlElementSetAttributeNode
此方法是文档对象模型 (DOM) 的Microsoft扩展。