System.Xml.XmlTextReader.ReadAttributeValue 方法

方法描述

将特性值分析为一个或多个 Text、EntityReference 或 EndEntity 节点。

语法定义(C# System.Xml.XmlTextReader.ReadAttributeValue 方法 的用法)

public override bool ReadAttributeValue()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Boolean 如果有可返回的节点,则为 true。 如果进行初始调用时读取器不是定位在特性节点上,或者如果已读取了所有特性值,则为 false。 如果是空特性(如 misc=""),则返回 true,同时为 String.Empty 的单个节点。

提示和注释

注意

在 .NET Framework 2.0 版 版本中,推荐的做法是使用 XmlReader.Create 方法创建 XmlReader 实例。 这使您可以充分利用此版本中引入的新功能。 有关更多信息,请参见 创建 XML 读取器。

调用 MoveToAttribute 后,使用此方法读取通过组成特性值的文本或实体引用节点。 特性值节点的 Depth 为 1 加上该特性节点的深度;当您进入和跳出常规实体引用时,它按 1 递增和递减。

System.Xml.XmlTextReader.ReadAttributeValue 方法例子

下面的示例读取具有文本和实体节点的特性。

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 ="";

       //Create the XmlParserContext.
       XmlParserContext context;
       string subset = "";
       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

异常

异常 异常描述

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。