Reading Attributes
The document is archived and information here might be outdated

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


Gets the number of attributes on the element.


Gets the value of the attribute.


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


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


Gets the value of the specified attribute.


Moves to the specified attribute.


Moves to the element that owns the current attribute node.


Moves to the first attribute.


Moves to the next attribute.


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.


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.


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.


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

// 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.

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

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.

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

string isbn = reader.GetAttribute("ISBN");
Console.WriteLine("The ISBN value: " + isbn);

See Also

© 2016 Microsoft