ReadAttributeValue Method

XmlNodeReader.ReadAttributeValue Method ()


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

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public override bool ReadAttributeValue()

Return Value

Type: System.Boolean

true if there are nodes to return.

false if the reader is not positioned on an attribute node when the initial call is made or if all the attribute values have been read.

An empty attribute, such as, misc="", returns true with a single node with a value of String.Empty.


In the .NET Framework 2.0, the recommended practice is to create XmlReader instances using the XmlReaderSettings class and the Create method. This allows you to take full advantage of all the new features introduced in the .NET Framework. For more information, see the Remarks section in the XmlReader reference page.

Use this method after calling MoveToAttribute to read through the text or entity reference nodes that make up the attribute value. The Depth of the attribute value nodes is one plus the depth of the attribute node; it increments and decrements by one when you step into and out of general entity references.

The following example reads an attribute with text and entity reference nodes.

using System;
using System.IO;
using System.Xml;

public class Sample 
  public static void Main()
    XmlNodeReader reader = null;

       //Create and load an XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'harcover'>]>" +
                   "<book genre='novel' misc='sale-item &h; 1987'>" +

       //Create the reader. 
       reader = new XmlNodeReader(doc);

       //Read the misc attribute. The attribute is parsed into multiple 
       //text and entity reference nodes.
       while (reader.ReadAttributeValue()){
          if (reader.NodeType==XmlNodeType.EntityReference)
            //To expand the entity, call ResolveEntity.
            Console.WriteLine("{0} {1}", reader.NodeType, reader.Name);
             Console.WriteLine("{0} {1}", reader.NodeType, reader.Value);
        if (reader != null)
} // End class

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft