AttributeCount Property

XmlTextReader.AttributeCount Property

Gets the number of attributes on the current node.

[Visual Basic]
Overrides Public ReadOnly Property AttributeCount As Integer
[C#]
public override int AttributeCount {get;}
[C++]
public: __property int get_AttributeCount();
[JScript]
public override function get AttributeCount() : int;

Property Value

The number of attributes on the current node.

Remarks

This property is relevant to Element, DocumentType and XmlDeclaration nodes only. (Other node types do not have attributes.)

Example

[Visual Basic, C#, C++] The following example displays all attributes on the current node.

[Visual Basic] 
Public Sub DisplayAttributes(reader As XmlReader)
    If reader.HasAttributes Then
        Console.WriteLine("Attributes of <" & reader.Name & ">")
        Dim i As Integer
        For i = 0 To reader.AttributeCount - 1
            reader.MoveToAttribute(i)
            Console.Write(" {0}={1}", reader.Name, reader.Value)
        Next i
        reader.MoveToElement() 'Moves the reader back to the element node.
    End If
End Sub 'DisplayAttributes

[C#] 
public void DisplayAttributes(XmlReader reader)
{
  if (reader.HasAttributes)
  {
    Console.WriteLine("Attributes of <" + reader.Name + ">");
    for (int i = 0; i < reader.AttributeCount; i++)
    {
      reader.MoveToAttribute(i);
      Console.Write(" {0}={1}", reader.Name, reader.Value);
    }
    reader.MoveToElement(); //Moves the reader back to the element node.
  }
}

[C++] 
public:
void DisplayAttributes(XmlReader* reader)
{
  if (reader->HasAttributes)
  {
    Console::WriteLine(S"Attributes of <{0}>", reader->Name);
    for (int i = 0; i < reader->AttributeCount; i++)
    {
      reader->MoveToAttribute(i);
      Console::Write(S" {0}={1}", reader->Name, reader->Value);
    }
    reader->MoveToElement(); //Moves the reader back to the element node.
  }
}

[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, Common Language Infrastructure (CLI) Standard

See Also

XmlTextReader Class | XmlTextReader Members | System.Xml Namespace

Show:
© 2016 Microsoft