Export (0) Print
Expand All

XmlElement.HasAttributes Property

Gets a boolean value indicating whether the current node has any attributes.

[Visual Basic]
Public Overridable ReadOnly Property HasAttributes As Boolean
[C#]
public virtual bool HasAttributes {get;}
[C++]
public: __property virtual bool get_HasAttributes();
[JScript]
public function get HasAttributes() : Boolean;

Property Value

true if the current node has attributes; otherwise, false.

Remarks

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

Example

[Visual Basic, C#, C++] The following example removes all attributes from the root element.

[Visual Basic] 
Imports System
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 all attributes.
    if (root.HasAttributes)
      root.RemoveAllAttributes()
    end if

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

  end sub
end class

[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 all attributes from the root element.
    if (root.HasAttributes)
      root.RemoveAllAttributes();
    
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>");

    XmlElement* root = doc->DocumentElement;

    // Remove all attributes from the root element.
    if (root->HasAttributes)
      root->RemoveAllAttributes();
    
    Console::WriteLine(S"Display the modified XML...");
    Console::WriteLine(doc->InnerXml);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlElement Class | XmlElement Members | System.Xml Namespace

Show:
© 2014 Microsoft