XmlReader.ReadContentAs(Type, IXmlNamespaceResolver) XmlReader.ReadContentAs(Type, IXmlNamespaceResolver) XmlReader.ReadContentAs(Type, IXmlNamespaceResolver) XmlReader.ReadContentAs(Type, IXmlNamespaceResolver) Method

定义

将内容作为指定类型的对象读取。Reads the content as an object of the type specified.

public:
 virtual System::Object ^ ReadContentAs(Type ^ returnType, System::Xml::IXmlNamespaceResolver ^ namespaceResolver);
public virtual object ReadContentAs (Type returnType, System.Xml.IXmlNamespaceResolver namespaceResolver);
abstract member ReadContentAs : Type * System.Xml.IXmlNamespaceResolver -> obj
override this.ReadContentAs : Type * System.Xml.IXmlNamespaceResolver -> obj
Public Overridable Function ReadContentAs (returnType As Type, namespaceResolver As IXmlNamespaceResolver) As Object

参数

returnType
Type Type Type Type

要返回的值的类型。The type of the value to be returned.

注意:随着 .NET Framework 3.5 的发布,returnType 参数的值现在可以为 DateTimeOffset 类型。Note With the release of the .NET Framework 3.5, the value of the returnType parameter can now be the DateTimeOffset type.

namespaceResolver
IXmlNamespaceResolver IXmlNamespaceResolver IXmlNamespaceResolver IXmlNamespaceResolver

一个 IXmlNamespaceResolver 对象,用于解析与类型转换有关的任何命名空间前缀。An IXmlNamespaceResolver object that is used to resolve any namespace prefixes related to type conversion. 例如,将 XmlQualifiedName 对象转换为 xs:string 时可以使用此对象。For example, this can be used when converting an XmlQualifiedName object to an xs:string.

此值可为 nullThis value can be null.

返回

已转换为请求类型的串联文本内容或属性值。The concatenated text content or attribute value converted to the requested type.

异常

内容格式不是目标类型的正确格式。The content is not in the correct format for the target type.

试图进行的强制转换无效。The attempted cast is not valid.

returnType 值为 nullThe returnType value is null.

当前节点不是所支持的节点类型。The current node is not a supported node type. 有关详细信息,请参见下表。See the table below for details.

- 或 --or-

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

读取 Decimal.MaxValueRead Decimal.MaxValue.

示例

下面的示例使用ReadContentAs方法将 colors 元素的内容返回到字符串对象数组中。The following example uses the ReadContentAs method to return the contents of the colors element into an array of string objects.

using (XmlReader reader = XmlReader.Create("dataFile_2.xml")) {
      reader.ReadToDescendant("item");

      reader.MoveToAttribute("colors");
      string[] colors = (string[]) reader.ReadContentAs(typeof(string[]),null);
      foreach (string color in colors) {
         Console.WriteLine("Colors: {0}", color);
      }             		
}
Using reader As XmlReader = XmlReader.Create("dataFile_2.xml")

  reader.ReadToDescendant("item")
              
  reader.MoveToAttribute("colors")
  Dim colors As String() = CType(reader.ReadContentAs(GetType(String()), Nothing), String())
  Dim color As String
  For Each color In  colors
    Console.WriteLine("Colors: {0}", color)
  Next color
          
End Using

示例使用 dataFile_2.xml 文件作为输入。The example uses the dataFile_2.xml file as input.

<root>
  <item sale-item='true' productID='123456' colors='blue green black'>
    <price>9.95</price>
  </item>
  <item sale-item='false' productID='124390'>
    <price>5.95</price>
  </item>
  <item sale-item='true' productID='53298'>
    <price>12.95</price>
  </item>
</root>

注解

此方法读取当前读取器位置处的文本内容,并将其转换为请求的返回类型。This method reads the text content at the current reader position and converts it to the requested return type. 文本、空白、有效空白和 CDATA 节串联在一起。Text, white space, significant white space and CDATA sections are concatenated. 跳过注释和处理指令,并自动解析实体引用。Comments and processing instructions are skipped and entity references are automatically resolved.

此方法用于读取、转换(如有必要),并从当前节点内容返回原子值项。This method is used to read, convert if necessary, and return atomic value items from the current node content. 如果输入类型是当前节点的类型的有效映射,则返回包含当前节点的值的目标类型的实例。If the input type is a valid mappings for the type of the current node then an instance of the target type containing the value of the current node is returned. 有关默认映射的列表, XmlReader请参阅 "参考" 页中的 "备注" 部分。See the Remarks section in the XmlReader reference page for a list of the default mappings.

例如,如果有以下 XML 文本:For example, if you had the following XML text:

<elem>123 <!-- comment --> <?pi my_text?> 456 <?pi another_pi?></elem>  

如果数据已类型化并向ReadContentAs方法调用提供字符串数组,则根据有效 CLR 类型映射的列表,将整数值转换为字符串。If the data is typed and a string array is supplied to the ReadContentAs method call, then the integer values are converted from strings according to the list of valid CLR type mappings.

如果数据是非类型化的,并且为ReadContentAs方法调用提供了字符串数组,则会将内容解析为单独的字符串。If the data is untyped and a string array is supplied to the ReadContentAs method call, then the content is parsed into separate strings. 返回一个包含两个字符串的数组,其值为 "123" 和 "456"。An array containing two strings is returned with the values "123" and "456". 不会保留内容中的空格。The spaces are not preserved from the content.

通常,在读取非类型化数据时,将根据提供的类型分析内容。In general when reading untyped data the content is parsed according to the supplied type. 例如,如果向ReadContentAs方法调用提供一个整数数组,则该字符串将被解析为一个整数{123,456}数组。For example, if an integer array is supplied to the ReadContentAs method call then the string is parsed into an array of integers {123,456}.

在下面的示例中,XML 文本不由空格分隔In the following example the XML text is not separated by spaces

<elem>123<!-- comment --><?pi my_text?>456789<?pi another_pi?></elem>  

如果内容是非类型化的,并且为ReadContentAs方法调用提供了字符串数组,则返回一个包含一个串联字符串的数组,其值为 "123456789"。If the content is untyped and a string array is supplied to the ReadContentAs method call then an array containing one concatenated string is returned with the value "123456789".

下表描述了此方法如何处理每个节点类型。The following table describes how this method treats each node type.

XmlNodeTypeXmlNodeType 返回值Return value 读取器行为Reader behavior
Text

CDATA

Whitespace

SignificantWhitespace

EntityReference

EndEntity
将文本、CDATA、空白和有效空白节点的串联内容转换为所请求的类型。Concatenated content of text, CDATA, white space and significant white space nodes converted to the requested type. 移至下一个开始元素或结束元素标记。Moves to the next start element or end element tag. 实体引用自动展开。Entity references are automatically expanded.
Attribute 与对属性XmlConvert.ToXxx值调用相同。Same as calling XmlConvert.ToXxx on the attribute value. 读取器仍保留在当前位置。The reader remains in the current position.
Comment

ProcessingInstruction
忽略处理指令 (PI) 或注释,读取 PI 或注释后面的串联文本内容。Ignores the processing instruction (PI) or comment and reads the concatenated text content that follows the PI or comment. 移至下一个开始元素或结束元素标记。Moves to the next start element or end element tag. 实体引用自动展开。Entity references are automatically expanded.
EndElement 空字符串。An empty string. 读取器仍保留在当前位置。The reader remains in the current position.
Element

XmlDeclaration

None

Document

DocumentType

Notation

Entity

DocumentFragment
引发 InvalidOperationExceptionAn InvalidOperationException is thrown. 未定义,尽管通常读取器将保留在当前位置。Undefined, although typically the reader remains in the current position.

有关详细信息,请参阅XmlReader引用页的 "备注" 部分和 W3C XML 架构第2部分:数据类型建议。For more information, see the Remarks section of the XmlReader reference page and the W3C XML Schema Part 2: Datatypes recommendation.

有关此方法的异步版本,请参阅ReadContentAsAsyncFor the asynchronous version of this method, see ReadContentAsAsync.

适用于