XmlElement.RemoveAttribute 方法

定义

移除指定的特性。Removes the specified attribute. (如果删除的特性具有默认值时,则将立即替换它)。(If the removed attribute has a default value, it is immediately replaced).

重载

RemoveAttribute(String)

按名称删除特性。Removes an attribute by name.

RemoveAttribute(String, String)

移除具有指定本地名称和命名空间 URI 的特性。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)

按名称删除特性。Removes an attribute by name.

public:
 virtual void RemoveAttribute(System::String ^ name);
public virtual void RemoveAttribute (string name);
abstract member RemoveAttribute : string -> unit
override this.RemoveAttribute : string -> unit
Public Overridable Sub RemoveAttribute (name As String)

参数

name
String

要移除的属性的名称。The name of the attribute to remove. 这是限定名。This is a qualified name. 它针对匹配节点的 Name 属性进行匹配。It is matched against the Name property of the matching node.

例外

该节点是只读的。The node is read-only.

示例

下面的示例从元素中移除一个特性。The following example removes an attribute from an element.

#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>" );
   XmlElement^ root = doc->DocumentElement;
   
   // Remove the genre attribute.
   root->RemoveAttribute( "genre" );
   Console::WriteLine( "Display the modified XML..." );
   Console::WriteLine( doc->InnerXml );
}

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);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")      

    Dim root as XmlElement = doc.DocumentElement

    ' Remove the genre attribute.
    root.RemoveAttribute("genre")

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

  end sub
end class

注解

如果已知删除的属性具有默认值,则会立即出现一个属性,其中包含默认值和相应的命名空间 URI、本地名称和前缀(如果适用)。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.

适用于

RemoveAttribute(String, String)

移除具有指定本地名称和命名空间 URI 的特性。Removes an attribute with the specified local name and namespace URI. (如果删除的特性具有默认值时,则将立即替换它)。(If the removed attribute has a default value, it is immediately replaced).

public:
 virtual void RemoveAttribute(System::String ^ localName, System::String ^ namespaceURI);
public virtual void RemoveAttribute (string localName, string namespaceURI);
public virtual void RemoveAttribute (string localName, string? namespaceURI);
abstract member RemoveAttribute : string * string -> unit
override this.RemoveAttribute : string * string -> unit
Public Overridable Sub RemoveAttribute (localName As String, namespaceURI As String)

参数

localName
String

要移除的特性的本地名称。The local name of the attribute to remove.

namespaceURI
String

要移除的特性的命名空间 URI。The namespace URI of the attribute to remove.

例外

该节点是只读的。The node is read-only.

示例

下面的示例从元素中移除一个特性。The following example removes an attribute from an element.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 );
}

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);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

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

    Dim root as XmlElement = doc.DocumentElement

    ' Remove the ISBN attribute.
    root.RemoveAttribute("ISBN", "urn:samples")
    
    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.InnerXml)

  end sub
end class

注解

如果已知删除的属性具有默认值,则会立即出现一个属性,其中包含默认值和相应的命名空间 URI、本地名称和前缀(如果适用)。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.

适用于