This documentation is archived and is not being maintained.

Reading Attributes with XmlReader

.NET Framework 1.1

Attributes are allowed on three node types:

  • Element
  • XmlDeclaration
  • DocumentType Declaration

For an element, movement through the attributes is different than reading the values of the attributes on an XmlDeclaration or DocumentType Declaration.

Reading Attributes on Elements

When positioned on an element node, the MoveToAttribute methods allow 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, not its containing element.

An attribute does not always have to be specified on an element. A DTD or schema can define a default value for attributes on elements. For example, if <!ATTLIST e a CDATA "123"> is in the DTD, the attribute "a" of element "e" is assigned the value "123" if the attribute "a" on the element "e" is not defined in the XML document. When using methods that move among attributes, attributes that receive value from a DTD or schema are acted upon just like attributes that are given a value in the XML stream. There is a programmatic way of determining how the attribute received its value, and that is by using the IsDefault property. The IsDefault property returns true if the current node is an attribute, and the attribute was not specified in the XML stream but instead came from the default attribute declaration in the DTD.

Note   Depending on the type of reader being used, this property does not always apply.

The following table shows what values the IsDefault property can return.

XmlReader implementationValues returned
XmlTextReaderfalse because no DTD information is present.
XmlValidatingReadertrue if the current node is an attribute whose value was generated from the default value that is defined in the DTD or schema. false if the value of the attribute was explicitly specified in the XML stream.
XmlNodeReadertrue if the DTD is defined in XmlDocument, false otherwise.
XsltReaderfalse because no DTD information (DOCTYPE nodes) is exposed in the XPath data model.

When positioned on an attribute, the GetAttribute method is used to retrieve the value of the attribute.

Attributes on Other Node Types

The XmlTextReader and XmlValidatingReader expose attributes only on Element, XmlDeclaration, and DocumentType nodes. When implementing XmlReader classes, for the XmlDeclaration node type, the Version, the Standalone, and the Encoding properties are returned as a string in the Value property of the XmlDeclaration. The Value property cannot be returned as string.Empty. If it is empty, the XmlDocument DOM class and other classes that may be dependent on this information cannot load from the XmlReader.

See Also

Reading XML with the XmlReader | Current Node Position in XmlReader | Property Settings on XmlReader | Object Comparison Using XmlNameTable with XmlReader | Reading Element and Attributes Content | Skipping Content with XmlReader | EntityReference Reading and Expansion | Comparing XmlReader to SAX Reader | Reading XML Data with XmlTextReader | Reading Node Trees with XmlNodeReader | Validating XML with XmlValidatingReader | Customized XML Reader Creation | XmlReader Class | XmlReader Members | XmlNodeReader Class | XmlNodeReader Members | XmlTextReader Class | XmlTextReader Members | XmlValidatingReader Class | XmlValidatingReader Members