.NET Framework Class Library
XmlTextReader..::.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)
Syntax

Visual Basic (Declaration)
Public Overrides Function ReadAttributeValue As Boolean
Visual Basic (Usage)
Dim instance As XmlTextReader
Dim returnValue As Boolean

returnValue = instance.ReadAttributeValue()
C#
public override bool ReadAttributeValue()
Visual C++
public:
virtual bool ReadAttributeValue() override
JScript
public override function ReadAttributeValue() : boolean

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

NoteNote:

In the .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the XmlReader..::.Create method. This allows you to take full advantage of the new features introduced in this release. For more information, see Creating XML Readers.

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.

Examples

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

Visual Basic
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim reader as XmlTextReader = nothing

    try
       'Create the XML fragment to be parsed.
       Dim xmlFrag as string ="<book genre='novel' misc='sale-item &h; 1987'></book>"

       'Create the XmlParserContext.
       Dim context as XmlParserContext 
       Dim subset as string = "<!ENTITY h 'hardcover'>"
       context = new XmlParserContext(nothing, nothing, "book", nothing, nothing, subset, "", "", XmlSpace.None)

       'Create the reader.
       reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)

       'Read the misc attribute. The attribute is parsed
       'into multiple text and entity reference nodes.
       reader.MoveToContent()
       reader.MoveToAttribute("misc")
       while (reader.ReadAttributeValue())
          if (reader.NodeType = XmlNodeType.EntityReference)
            Console.WriteLine("{0} {1}", reader.NodeType, reader.Name)
          else
             Console.WriteLine("{0} {1}", reader.NodeType, reader.Value)
          end if
        end while

     finally 
       if Not reader Is Nothing
        reader.Close()
      End if
      end try
  end sub
end class 
C#
using System;
using System.IO;
using System.Xml;

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

    try
    {
       //Create the XML fragment to be parsed.
       string xmlFrag ="<book genre='novel' misc='sale-item &h; 1987'></book>";

       //Create the XmlParserContext.
       XmlParserContext context;
       string subset = "<!ENTITY h 'hardcover'>";
       context = new XmlParserContext(null, null, "book", null, null, subset, "", "", XmlSpace.None);

       //Create the reader.
       reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

       //Read the misc attribute. The attribute is parsed
       //into multiple text and entity reference nodes.
       reader.MoveToContent();
       reader.MoveToAttribute("misc");
       while (reader.ReadAttributeValue()){
          if (reader.NodeType==XmlNodeType.EntityReference)
            Console.WriteLine("{0} {1}", reader.NodeType, reader.Name);
          else
             Console.WriteLine("{0} {1}", reader.NodeType, reader.Value);
        } 
     } 
     finally 
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Visual C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   __try
   {

      // Create the XML fragment to be parsed.
      String^ xmlFrag = "<book genre='novel' misc='sale-item &h; 1987'></book>";

      // Create the XmlParserContext.
      XmlParserContext^ context;
      String^ subset = "<!ENTITY h 'hardcover'>";
      context = gcnew XmlParserContext( nullptr,nullptr,"book",nullptr,nullptr,subset,"","",XmlSpace::None );

      // Create the reader.
      reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );

      // Read the misc attribute. The attribute is parsed
      // into multiple text and entity reference nodes.
      reader->MoveToContent();
      reader->MoveToAttribute( "misc" );
      while ( reader->ReadAttributeValue() )
      {
         if ( reader->NodeType == XmlNodeType::EntityReference )
                  Console::WriteLine( " {0} {1}", reader->NodeType, reader->Name );
         else
                  Console::WriteLine( " {0} {1}", reader->NodeType, reader->Value );
      }
   }
   __finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}

CPP_OLD
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main() 
{
   XmlTextReader* reader = 0;

   __try
   {
      // Create the XML fragment to be parsed.
      String* xmlFrag =S"<book genre='novel' misc='sale-item &h; 1987'></book>";

      // Create the XmlParserContext.
      XmlParserContext * context;
      String* subset = S"<!ENTITY h 'hardcover'>";
      context = new XmlParserContext(0, 0, S"book", 0, 0, subset, S"", S"", XmlSpace::None);

      // Create the reader.
      reader = new XmlTextReader(xmlFrag, XmlNodeType::Element, context);

      // Read the misc attribute. The attribute is parsed
      // into multiple text and entity reference nodes.
      reader -> MoveToContent();
      reader -> MoveToAttribute(S"misc");
      while (reader -> ReadAttributeValue())
      {
         if (reader -> NodeType == XmlNodeType::EntityReference)
            Console::WriteLine(S" {0} {1}", __box(reader -> NodeType), reader -> Name);
         else
            Console::WriteLine(S" {0} {1}", __box(reader -> NodeType), reader -> Value);
      }
   }
   __finally
   {
      if (reader != 0)
         reader -> Close();
   }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker