XmlDocument.CreateAttribute 方法

定義

建立 XmlAttribute 一個名稱指定的 。

多載

名稱 Description
CreateAttribute(String)

建立XmlAttribute一個,且指定的。Name

CreateAttribute(String, String)

建立 XmlAttribute 一個具有指定限定名稱且 NamespaceURI的 。

CreateAttribute(String, String, String)

創造 XmlAttribute 一個,指定為 PrefixLocalNameNamespaceURI和 。

CreateAttribute(String)

來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs

建立XmlAttribute一個,且指定的。Name

public:
 System::Xml::XmlAttribute ^ CreateAttribute(System::String ^ name);
public System.Xml.XmlAttribute CreateAttribute(string name);
member this.CreateAttribute : string -> System.Xml.XmlAttribute
Public Function CreateAttribute (name As String) As XmlAttribute

參數

name
String

屬性的限定名稱。 若名稱包含冒號,屬性 Prefix 會反映名字中第一個冒號前的部分,屬性 LocalName 則反映名字中第一個冒號之後的部分。 除非前綴是被認可的內建前綴(如 xmlns),否則 p NamespaceURI 保持空。 此時 NamespaceURI 的值為 http://www.w3.org/2000/xmlns/

傳回

新的 XmlAttribute.

範例

以下步驟建立一個屬性並將其加入 XML 文件中。

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可以用該XmlElement方法加入 。SetAttributeNode

適用於

CreateAttribute(String, String)

來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs

建立 XmlAttribute 一個具有指定限定名稱且 NamespaceURI的 。

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.XmlAttribute
Public Function CreateAttribute (qualifiedName As String, namespaceURI As String) As XmlAttribute

參數

qualifiedName
String

屬性的限定名稱。 若名稱包含冒號,屬性 Prefix 會反映冒號前的部分,屬性 LocalName 則反映冒號後的部分。

namespaceURI
String

屬性的 namespaceURI。 若限定名稱包含 xmlns 的前綴,則此參數必須為 http://www.w3.org/2000/xmlns/

傳回

新的 XmlAttribute.

備註

XmlAttribute可以用該XmlElement方法加入 。SetAttributeNode

適用於

CreateAttribute(String, String, String)

來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs
來源:
XmlDocument.cs

創造 XmlAttribute 一個,指定為 PrefixLocalNameNamespaceURI和 。

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.XmlAttribute
Public 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可以用該XmlElement方法加入 。SetAttributeNode

此方法是 Microsoft 對文件物件模型(DOM)的擴充。

適用於