Reading Attributes

The XmlReader class provides various methods and properties for reading attributes. Attributes are most commonly found on elements. However, they are also allowed on XML declaration and document type nodes.

Reading Attributes on Elements

When positioned on an element node, the MoveToAttribute methods enable you to go through the attribute list of the element. After MoveToAttribute has been called, the node properties—such as Name, NamespaceURI, Prefix, and so on—reflect the properties of that attribute, and not the containing element it belongs to.

The following table describes the methods and properties that are designed specifically to process attributes.

Member Name

Description

AttributeCount

Gets the number of attributes on the element.

GetAttribute

Gets the value of the attribute.

HasAttributes

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

IsDefault

Gets a value indicating whether the current node is an attribute that was generated from the default value defined in the DTD or schema.

Item

Gets the value of the specified attribute.

MoveToAttribute

Moves to the specified attribute.

MoveToElement

Moves to the element that owns the current attribute node.

MoveToFirstAttribute

Moves to the first attribute.

MoveToNextAttribute

Moves to the next attribute.

ReadAttributeValue

Parses the attribute value into one or more Text, EntityReference, or EndEntity nodes.

Any of the general XmlReader methods and properties can also be used to process attributes. For example, after the XmlReader is positioned on an attribute, the Name and Value properties reflect the values of the attribute. You can also use any of the content Read methods to get the value of the attribute.

Reading Attributes on Other Node Types

Processing attributes on element nodes is the most common scenario. Attributes can also be found on XML Declarations and document type declarations.

Note

When the XmlReader is positioned on a processing instruction node, the Value property returns the entire text content. Items in the processing instruction node are not treated as attributes. They cannot be read with the GetAttribute or MoveToAttribute methods.

XML Declaration Node

When positioned on an XML Declaration node, the version, standalone, and encoding information are returned as a single string by the Value property. On some readers the version, encoding and stand-alone information can be also exposed as attributes.

Note

XmlReader objects created by the Create method, the XmlTextReader, and XmlValidatingReader classes, expose the version, stand-alone, and encoding items as attributes.

Document Type Node

When the XmlReader is positioned on a Document Type node, the GetAttribute method and Item property can be used to return the values for the SYSTEM and PUBLIC literals. For example, calling reader.GetAttribute("PUBLIC") returns the PUBLIC value.

Examples

The following example reads all attributes on an element using the AttributeCount property.

' Display all attributes. 
If reader.HasAttributes Then
  Console.WriteLine("Attributes of <" + reader.Name + ">")
  Dim i As Integer 
  For i = 0 To (reader.AttributeCount - 1)
    Console.WriteLine("  {0}", reader(i))
  Next i
  ' Move the reader back to the element node.
  reader.MoveToElement() 
End If
// Display all attributes. 
if (reader.HasAttributes) {
  Console.WriteLine("Attributes of <" + reader.Name + ">");
  for (int i = 0; i < reader.AttributeCount; i+) {
    Console.WriteLine("  {0}", reader[i]);
  }
  // Move the reader back to the element node.
  reader.MoveToElement(); 
}

The following example reads all attributes on an element using the MoveToNextAttribute method in a While loop.

If reader.HasAttributes Then
  Console.WriteLine("Attributes of <" + reader.Name + ">")
  While reader.MoveToNextAttribute()
    Console.WriteLine(" {0}={1}", reader.Name, reader.Value)
  End While 
  ' Move the reader back to the element node.
  reader.MoveToElement()
End If
if (reader.HasAttributes) {
  Console.WriteLine("Attributes of <" + reader.Name + ">");
  while (reader.MoveToNextAttribute()) {
    Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
  }
  // Move the reader back to the element node.
  reader.MoveToElement();
}

The following example gets the value of an attribute by name.

reader.ReadToFollowing("book")
Dim isbn As String = reader.GetAttribute("ISBN")
Console.WriteLine("The ISBN value: " + isbn)
reader.ReadToFollowing("book");
string isbn = reader.GetAttribute("ISBN");
Console.WriteLine("The ISBN value: " + isbn);

See Also

Concepts

Reading XML with the XmlReader