Läs på engelska Redigera

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

C#
public System.Xml.XmlAttribute CreateAttribute(string name);

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.

C#
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);
  }
}

Remarks

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

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

CreateAttribute(String, String)

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

Creates an XmlAttribute with the specified qualified name and NamespaceURI.

C#
public System.Xml.XmlAttribute CreateAttribute(string qualifiedName, string namespaceURI);
C#
public System.Xml.XmlAttribute CreateAttribute(string qualifiedName, string? namespaceURI);

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

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

CreateAttribute(String, String, String)

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

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

C#
public virtual System.Xml.XmlAttribute CreateAttribute(string prefix, string localName, string namespaceURI);
C#
public virtual System.Xml.XmlAttribute CreateAttribute(string? prefix, string localName, string? namespaceURI);

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

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0