XmlReader.ReadContentAsDateTime 方法

定义

将当前位置的文本内容作为 DateTime 对象读取。Reads the text content at the current position as a DateTime object.

public:
 virtual DateTime ReadContentAsDateTime();
public virtual DateTime ReadContentAsDateTime ();
abstract member ReadContentAsDateTime : unit -> DateTime
override this.ReadContentAsDateTime : unit -> DateTime
Public Overridable Function ReadContentAsDateTime () As DateTime

返回

DateTime

作为 DateTime 对象的文本内容。The text content as a DateTime 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.

如果内容是类型化的 xsd:dateTime ,则读取器将返回取消装箱的 DateTime 对象。If the content is typed xsd:dateTime, the reader returns an unboxed DateTime object. 如果未键入内容 xsd:dateTime ,读取器将尝试 DateTime 根据 W3C XML 架构第2部分:数据类型 建议中定义的规则将其转换为对象。If the content is not typed xsd:dateTime, the reader attempts to convert it to a DateTime object according to the rules defined by the W3C XML Schema Part 2: Datatypes recommendation.

备注

如果内容类型化为 DateTime.Year,则不能使用 xsd:gMonthDay 值。You cannot rely on the DateTime.Year value when the content is typed as xsd:gMonthDay. XmlReaderDateTime.Year在这种情况下,始终将值设置为1904。XmlReader always sets the DateTime.Year value to 1904 in this case.

下表描述了此方法如何处理每个节点类型。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 XmlReader reference page and the W3C XML Schema Part 2: Datatypes recommendation.

适用于