XmlReader.ReadContentAsObject XmlReader.ReadContentAsObject XmlReader.ReadContentAsObject XmlReader.ReadContentAsObject Method

定义

将当前位置的文本内容作为 Object 读取。Reads the text content at the current position as an Object.

public:
 virtual System::Object ^ ReadContentAsObject();
public virtual object ReadContentAsObject ();
abstract member ReadContentAsObject : unit -> obj
override this.ReadContentAsObject : unit -> obj
Public Overridable Function ReadContentAsObject () As Object

返回

作为最适当的公共语言运行时 (CLR) 对象的文本内容。The text content as the most appropriate common language runtime (CLR) object.

异常

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

该字符串格式无效。The string format is not valid.

在上一次异步操作完成之前调用了 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."

注解

此方法连接文本、空格、有效空白和 CDATA 部分,并跳过任何注释或处理指令。This method concatenates text, white space, significant white space, and CDATA sections, and skips any comments or processing instructions. 实体引用会自动得到解决。Entity references are automatically resolved.

如果内容是类型化的,则读取器将返回由ValueType属性指定的最适合类型的已装箱 CLR。If the content is typed, the reader returns a boxed CLR of the most appropriate type, specified by the ValueType property. 如果内容是列表类型,则读取器将返回适当类型的装箱对象的数组。If the content is a list type, the reader returns an array of boxed objects of the appropriate type.

备注

如果在分析内容时出现验证错误,并且读取器是XmlReader Create由方法创建的对象,则读取器将内容作为字符串返回。If a validation error occurs while parsing the content and the reader is an XmlReader object created by the Create method, the reader returns the content as a string. 换句话说,当发生验证错误或警告时,内容将被视为非类型化的。In other words when a validation error or warning occurs, the content is considered to be untyped.

如果内容未类型化,读取器将以字符串的形式返回内容。If the content is not typed, the reader returns the content as a string.

下表描述了此方法如何处理每个节点类型。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 如果读取器是架构验证读取器(ValidationType设置为Schema),则为元素的值; 否则为空字符串。The value of the element if the reader is a schema validating reader (ValidationType is set to Schema); otherwise 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.

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

适用于