Editar

Partilhar via


XmlDocument.CreateAttribute Method

Definition

Creates an XmlAttribute with the specified name.

Overloads

CreateAttribute(String)

Creates an XmlAttribute with the specified Name.

CreateAttribute(String, String)

Creates an XmlAttribute with the specified qualified name and NamespaceURI.

CreateAttribute(String, String, String)

Creates an XmlAttribute with the specified Prefix, LocalName, and NamespaceURI.

CreateAttribute(String)

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

Creates 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

Parameters

name
String

The qualified name of the attribute. 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. The NamespaceURI remains empty unless the prefix is a recognized built-in prefix such as xmlns. In this case NamespaceURI has a value of http://www.w3.org/2000/xmlns/.

Returns

The new XmlAttribute.

Examples

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

Remarks

The XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

Applies to

CreateAttribute(String, String)

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

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

Parameters

qualifiedName
String

The qualified name of the attribute. 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

The namespaceURI of the attribute. If the qualified name includes a prefix of xmlns, then this parameter must be http://www.w3.org/2000/xmlns/.

Returns

The new XmlAttribute.

Remarks

The XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

Applies to

CreateAttribute(String, String, String)

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

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

Parameters

prefix
String

The prefix of the attribute (if any). String.Empty and null are equivalent.

localName
String

The local name of the attribute.

namespaceURI
String

The namespace URI of the attribute (if any). String.Empty and null are equivalent. If prefix is xmlns, then this parameter must be http://www.w3.org/2000/xmlns/; otherwise an exception is thrown.

Returns

The new XmlAttribute.

Remarks

The XmlAttribute can be added to an XmlElement using the SetAttributeNode method.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to