XmlDocument.CreateAttribute 方法

定義

建立具有指定名稱的 XmlAttribute

多載

CreateAttribute(String)

建立具有指定 NameXmlAttribute

CreateAttribute(String, String)

建立具有指定限定名稱和 NamespaceURIXmlAttribute

CreateAttribute(String, String, String)

建立具有指定的 PrefixLocalNameNamespaceURIXmlAttribute

CreateAttribute(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

建立具有指定 NameXmlAttribute

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

屬性的限定名稱 (Qualified Name)。 如果名稱包含冒號,Prefix 屬性會反映第一個冒號之前的名稱部分,而 LocalName 屬性會反映第一個冒號之後的名稱部分。 除非前置詞能夠辨認為內建前置詞 (例如 xmlns),否則 NamespaceURI 會保持空白。 在此情況下,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

建立具有指定限定名稱和 NamespaceURIXmlAttribute

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

屬性的限定名稱 (Qualified Name)。 如果名稱包含冒號,Prefix 屬性會反映冒號前面的名稱部分,而 LocalName 屬性會反映冒號後面的名稱部分。

namespaceURI
String

屬性的命名空間 URI。 如果限定名稱包含 xmlns 前置詞,則此參數必須是 http://www.w3.org/2000/xmlns/

傳回

新的 XmlAttribute

備註

XmlAttribute可以使用 方法將 加入至 XmlElementSetAttributeNode

適用於

CreateAttribute(String, String, String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

建立具有指定的 PrefixLocalNameNamespaceURIXmlAttribute

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可以使用 方法將 加入至 XmlElementSetAttributeNode

這個方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於