XmlElement.RemoveAttribute Method

Definition

Removes the specified attribute. (If the removed attribute has a default value, it is immediately replaced).

Overloads

RemoveAttribute(String)

Removes an attribute by name.

RemoveAttribute(String, String)

Removes an attribute with the specified local name and namespace URI. (If the removed attribute has a default value, it is immediately replaced).

RemoveAttribute(String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

Removes an attribute by name.

C#
public virtual void RemoveAttribute(string name);

Parameters

name
String

The name of the attribute to remove. This is a qualified name. It is matched against the Name property of the matching node.

Exceptions

The node is read-only.

Examples

The following example removes an attribute from an element.

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

    XmlElement root = doc.DocumentElement;

    // Remove the genre attribute.
    root.RemoveAttribute("genre");

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);
  }
}

Remarks

If the removed attribute is known to have a default value, an attribute immediately appears containing the default value and, if applicable, the corresponding namespace URI, local name, and prefix.

Applies to

.NET 10 and other versions
Product Versions
.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

RemoveAttribute(String, String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

Removes an attribute with the specified local name and namespace URI. (If the removed attribute has a default value, it is immediately replaced).

C#
public virtual void RemoveAttribute(string localName, string namespaceURI);
C#
public virtual void RemoveAttribute(string localName, string? namespaceURI);

Parameters

localName
String

The local name of the attribute to remove.

namespaceURI
String

The namespace URI of the attribute to remove.

Exceptions

The node is read-only.

Examples

The following example removes an attribute from an element.

C#
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    // Remove the ISBN attribute.
    root.RemoveAttribute("ISBN", "urn:samples");

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);
  }
}

Remarks

If the removed attribute is known to have a default value, an attribute immediately appears containing the default value and, if applicable, the corresponding namespace URI, local name, and prefix.

Applies to

.NET 10 and other versions
Product Versions
.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