XmlDocument.CreateAttribute XmlDocument.CreateAttribute XmlDocument.CreateAttribute XmlDocument.CreateAttribute Method

定義

建立具有指定名稱的 XmlAttributeCreates an XmlAttribute with the specified name.

多載

CreateAttribute(String) CreateAttribute(String) CreateAttribute(String) CreateAttribute(String)

建立具有指定 NameXmlAttributeCreates an XmlAttribute with the specified Name.

CreateAttribute(String, String) CreateAttribute(String, String) CreateAttribute(String, String) CreateAttribute(String, String)

建立具有指定限定名稱和 NamespaceURIXmlAttributeCreates an XmlAttribute with the specified qualified name and NamespaceURI.

CreateAttribute(String, String, String) CreateAttribute(String, String, String) CreateAttribute(String, String, String) CreateAttribute(String, String, String)

建立具有指定的 PrefixLocalNameNamespaceURIXmlAttributeCreates an XmlAttribute with the specified Prefix, LocalName, and NamespaceURI.

CreateAttribute(String) CreateAttribute(String) CreateAttribute(String) CreateAttribute(String)

建立具有指定 NameXmlAttributeCreates an XmlAttribute with the specified 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 String String String

屬性的限定名稱 (Qualified Name)。The qualified name of the attribute. 如果名稱包含冒號,Prefix 屬性會反映第一個冒號之前的名稱部分,而 LocalName 屬性會反映第一個冒號之後的名稱部分。If the name contains a colon, the Prefix property reflects the part of the name preceding the first colon and the LocalName property reflects the part of the name following the first colon. 除非前置詞能夠辨認為內建前置詞 (例如 xmlns),否則 NamespaceURI 會保持空白。The NamespaceURI remains empty unless the prefix is a recognized built-in prefix such as xmlns. 在此情況下,NamespaceURI 的值為 http://www.w3.org/2000/xmlns/In this case NamespaceURI has a value of http://www.w3.org/2000/xmlns/.

傳回

新的 XmlAttributeThe new XmlAttribute.

範例

下列程式會建立屬性, 並將它加入至 XML 檔。The following creates an attribute and adds it to an XML document.

#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可以XmlElement使用方法SetAttributeNode新增至。The XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

CreateAttribute(String, String) CreateAttribute(String, String) CreateAttribute(String, String) CreateAttribute(String, String)

建立具有指定限定名稱和 NamespaceURIXmlAttributeCreates an XmlAttribute with the specified qualified name and NamespaceURI.

public:
 System::Xml::XmlAttribute ^ CreateAttribute(System::String ^ qualifiedName, System::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 String String String

屬性的限定名稱 (Qualified Name)。The qualified name of the attribute. 如果名稱包含冒號,Prefix 屬性會反映冒號前面的名稱部分,而 LocalName 屬性會反映冒號後面的名稱部分。If the name contains a colon then the Prefix property will reflect the part of the name preceding the colon and the LocalName property will reflect the part of the name after the colon.

namespaceURI
String String String String

屬性的命名空間 URI。The namespaceURI of the attribute. 如果限定名稱包含 xmlns 前置詞,則此參數必須是 http://www.w3.org/2000/xmlns/If the qualified name includes a prefix of xmlns, then this parameter must be http://www.w3.org/2000/xmlns/.

傳回

新的 XmlAttributeThe new XmlAttribute.

備註

XmlAttribute可以XmlElement使用方法SetAttributeNode新增至。The XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

CreateAttribute(String, String, String) CreateAttribute(String, String, String) CreateAttribute(String, String, String) CreateAttribute(String, String, String)

建立具有指定的 PrefixLocalNameNamespaceURIXmlAttributeCreates an XmlAttribute with the specified Prefix, LocalName, and NamespaceURI.

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);
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 String String

屬性的前置詞 (如有此項)。The prefix of the attribute (if any). String.Empty 與 null 相等。String.Empty and null are equivalent.

localName
String String String String

屬性的本機名稱。The local name of the attribute.

namespaceURI
String String String String

屬性的命名空間 URI (如有此項)。The namespace URI of the attribute (if any). String.Empty 與 null 相等。String.Empty and null are equivalent. 如果 prefix 是 xmlns,則這個參數必須是 http://www.w3.org/2000/xmlns/;否則會擲回例外狀況。If prefix is xmlns, then this parameter must be http://www.w3.org/2000/xmlns/; otherwise an exception is thrown.

傳回

新的 XmlAttributeThe new XmlAttribute.

備註

XmlAttribute可以XmlElement使用方法SetAttributeNode新增至。The XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

適用於