XmlReader.ReadElementContentAsDateTime XmlReader.ReadElementContentAsDateTime XmlReader.ReadElementContentAsDateTime XmlReader.ReadElementContentAsDateTime Method

定义

读取当前元素并将内容作为 DateTime 对象返回。Reads the current element and returns the contents as a DateTime object.

重载

ReadElementContentAsDateTime() ReadElementContentAsDateTime() ReadElementContentAsDateTime() ReadElementContentAsDateTime()

读取当前元素并将内容作为 DateTime 对象返回。Reads the current element and returns the contents as a DateTime object.

ReadElementContentAsDateTime(String, String) ReadElementContentAsDateTime(String, String) ReadElementContentAsDateTime(String, String) ReadElementContentAsDateTime(String, String)

检查指定的本地名称和命名空间 URI 与当前元素的本地名称和命名空间 URI 是否匹配,然后读取当前元素,并将内容作为 DateTime 对象返回。Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a DateTime object.

ReadElementContentAsDateTime() ReadElementContentAsDateTime() ReadElementContentAsDateTime() ReadElementContentAsDateTime()

读取当前元素并将内容作为 DateTime 对象返回。Reads the current element and returns the contents as a DateTime object.

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

返回

作为 DateTime 对象的元素内容。The element content as a DateTime object.

异常

XmlReader 未定位在元素上。The XmlReader is not positioned on an element.

-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."

当前元素包含子元素。The current element contains child elements.

-or- 无法将元素内容转换为 DateTime 对象。The element content cannot be converted to a DateTime object.

使用 null 参数调用此方法。The method is called with null arguments.

示例

下面的示例使用ReadElementContentAsDateTime方法读取date元素的内容。The following example uses the ReadElementContentAsDateTime method to read the content of the date element.

using (XmlReader reader = XmlReader.Create("dataFile.xml")) {
     reader.ReadToFollowing("date");
     DateTime date = reader.ReadElementContentAsDateTime();
  
     // If the current culture is "en-US",
     // this writes "Wednesday, January 8, 2003".
     Console.WriteLine(date.ToLongDateString());
}
Using reader As XmlReader = XmlReader.Create("dataFile.xml")
  reader.ReadToFollowing("date")
  Dim [date] As DateTime = reader.ReadElementContentAsDateTime()
            
  ' If the current culture is "en-US",
  ' this writes "Wednesday, January 8, 2003".
  Console.WriteLine([date].ToLongDateString())
End Using

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

<root>
  <stringValue>
     <!--comment-->
     <?some pi?>
      text value of the element.
  </stringValue>
  <longValue>270000000000001</longValue>
  <number>0</number>
  <double>2E10</double>
  <date>2003-01-08T15:00:00-00:00</date>
</root>

注解

此方法读取开始标记和元素的内容,并将读取器移动到结束元素标记之后。This method reads the start tag, the contents of the element, and moves the reader past the end element tag. 它扩展实体并忽略处理指令和注释。It expands entities and ignores processing instructions and comments. 元素只能包含简单内容。The element can only contain simple content. 也就是说,它不能有子元素。That is, it cannot have child elements.

如果内容是类型化xsd:dateTime的,则读取器将DateTime返回取消装箱的对象。If the content is typed xsd:dateTime, the reader returns an unboxed DateTime object. 如果未键入xsd:dateTime内容,读取器将尝试根据W3C XML 架构第 2 DateTime部分定义的规则将其转换为对象:数据类型建议。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. XmlReader在这种DateTime.Year情况下,始终将值设置为1904。XmlReader always sets the DateTime.Year value to 1904 in this case.

有关详细信息,请参阅 XmlReader 引用页的“备注”部分。For more information, see the Remarks section of the XmlReader reference page.

ReadElementContentAsDateTime(String, String) ReadElementContentAsDateTime(String, String) ReadElementContentAsDateTime(String, String) ReadElementContentAsDateTime(String, String)

检查指定的本地名称和命名空间 URI 与当前元素的本地名称和命名空间 URI 是否匹配,然后读取当前元素,并将内容作为 DateTime 对象返回。Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a DateTime object.

public:
 virtual DateTime ReadElementContentAsDateTime(System::String ^ localName, System::String ^ namespaceURI);
public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI);
abstract member ReadElementContentAsDateTime : string * string -> DateTime
override this.ReadElementContentAsDateTime : string * string -> DateTime
Public Overridable Function ReadElementContentAsDateTime (localName As String, namespaceURI As String) As DateTime

参数

localName
String String String String

元素的本地名称。The local name of the element.

namespaceURI
String String String String

元素的命名空间 URI。The namespace URI of the element.

返回

作为 DateTime 对象的元素内容。The element contents as a DateTime object.

异常

XmlReader 未定位在元素上。The XmlReader is not positioned on an element.

- 或 --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."

当前元素包含子元素。The current element contains child elements.

-or-

无法将元素内容转换成请求的类型。The element content cannot be converted to the requested type.

使用 null 参数调用此方法。The method is called with null arguments.

指定的本地名称和命名空间 URI 与所读取的当前元素的本地名称和命名空间 URI 不匹配。The specified local name and namespace URI do not match that of the current element being read.

注解

此方法读取开始标记和元素的内容,并将读取器移动到结束元素标记之后。This method reads the start tag, the contents of the element, and moves the reader past the end element tag. 它扩展实体并忽略处理指令和注释。It expands entities and ignores processing instructions and comments. 元素只能包含简单内容。The element can only contain simple content. 也就是说,它不能有子元素。That is, it cannot have child elements.

如果内容是类型化xsd:dateTime的,则读取器将DateTime返回取消装箱的对象。If the content is typed xsd:dateTime, the reader returns an unboxed DateTime object. 如果未键入xsd:dateTime内容,读取器将尝试根据W3C XML 架构第 2 DateTime部分定义的规则将其转换为对象:数据类型建议。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. XmlReader在这种DateTime.Year情况下,始终将值设置为1904。XmlReader always sets the DateTime.Year value to 1904 in this case.

有关详细信息,请参阅 XmlReader 引用页的“备注”部分。For more information, see the Remarks section of the XmlReader reference page.

适用于