XmlDocument.CreateAttribute 方法

定义

创建具有指定名称的 XmlAttributeCreates an XmlAttribute with the specified name.

重载

CreateAttribute(String)

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

CreateAttribute(String, String)

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

CreateAttribute(String, String, String)

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

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

属性的限定名称。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. NamespaceURI 保持为空,除非该前缀是一个可识别的内置前缀,例如 xmlns。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

注解

可以使用 SetAttributeNode 方法将 XmlAttribute 添加到 XmlElementThe XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

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

属性的限定名称。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

属性的 namespaceURI。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.

注解

可以使用 SetAttributeNode 方法将 XmlAttribute 添加到 XmlElementThe XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

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

属性的前缀(如果有的话)。The prefix of the attribute (if any). String.Empty 与 null 等效。String.Empty and null are equivalent.

localName
String

属性的本地名称。The local name of the attribute.

namespaceURI
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.

注解

可以使用 SetAttributeNode 方法将 XmlAttribute 添加到 XmlElementThe 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).

适用于