Export (0) Print
Expand All

XmlValidatingReader.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 ()
public boolean ReadAttributeValue ()
public override function ReadAttributeValue () : boolean
Not applicable.

Return Value

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.

NoteNote:

The XmlValidatingReader class is obsolete in Microsoft .NET Framework version 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see Validating XML Data with XmlReader.

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. The Depth increments and decrements by one when you step into and out of general entity references.

For example, suppose you have the following XML: <test name="a &b; c"/>

where the entity b is defined in the document type definition (DTD) as follows: <!ENTITY b "123">

If EntityHandling is set to ExpandCharEntities, the following C# code returns the attribute value as two text nodes and one entity reference node:

reader.MoveToAttribute("name");
  while (reader.ReadAttributeValue())
  {
  if (reader.NodeType == XmlNodeType.Text)
  {
  // at this point reader.Value == "a " or " c"
  }
  else if (reader.NodeType == XmlNodeType.EntityReference)
  {
  // at this point reader.Name == "b"
  reader.ResolveEntity();
  while (reader.ReadAttributeValue() &&
  reader.NodeType != XmlNodeType.EndEntity)
  {
  // reader.Value == "123"
  }
  }
    }

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()
  {
    XmlValidatingReader 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 and set it to not expand general entities. 
       reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
       reader.ValidationType = ValidationType.None;
       reader.EntityHandling = EntityHandling.ExpandCharEntities;
  
       //Read the misc attribute. Because EntityHandling is set to
       //ExpandCharEntities, the attribute is parsed into multiple text
       //and entity reference nodes.
       reader.MoveToContent();
       reader.MoveToAttribute("misc");
       while (reader.ReadAttributeValue()){
          if (reader.NodeType==XmlNodeType.EntityReference)
            //To expand the entity, call ResolveEntity.
            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

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{   
    public static void main(String[] args)
    {
        XmlValidatingReader 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 and set it to not expand general entities. 
            reader = new XmlValidatingReader
                (xmlFrag, XmlNodeType.Element, context);
            reader.set_ValidationType(ValidationType.None);
            reader.set_EntityHandling(EntityHandling.ExpandCharEntities);

            //Read the misc attribute. Because EntityHandling is set to
            //ExpandCharEntities, the attribute is parsed into multiple text
            //and entity reference nodes.
            reader.MoveToContent();
            reader.MoveToAttribute("misc");
            while(reader.ReadAttributeValue()) {
                if (reader.get_NodeType().Equals
					(XmlNodeType.EntityReference)) {
                    //To expand the entity, call ResolveEntity.
                    Console.WriteLine("{0} {1}", reader.get_NodeType(),
                        reader.get_Name());
                }
                else {
                    Console.WriteLine("{0} {1}", reader.get_NodeType(), 
                        reader.get_Value());
                }
            }
        }
        finally {
            if (reader != null) {
                reader.Close();
            }
        }
    } //main
} //End class Sample

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft