XmlReader.ReadElementContentAs XmlReader.ReadElementContentAs XmlReader.ReadElementContentAs XmlReader.ReadElementContentAs Method

定义

读取当前元素,并将内容作为指定类型的对象返回。Reads the current element and returns the contents as an object of the type specified.

重载

ReadElementContentAs(Type, IXmlNamespaceResolver) ReadElementContentAs(Type, IXmlNamespaceResolver) ReadElementContentAs(Type, IXmlNamespaceResolver) ReadElementContentAs(Type, IXmlNamespaceResolver)

将元素内容作为请求类型读取。Reads the element content as the requested type.

ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) ReadElementContentAs(Type, IXmlNamespaceResolver, String, String)

检查指定的本地名称和命名空间 URI 与当前元素的本地名称和命名空间 URI 是否匹配,然后将元素内容作为请求类型读取。Checks that the specified local name and namespace URI matches that of the current element, then reads the element content as the requested type.

ReadElementContentAs(Type, IXmlNamespaceResolver) ReadElementContentAs(Type, IXmlNamespaceResolver) ReadElementContentAs(Type, IXmlNamespaceResolver) ReadElementContentAs(Type, IXmlNamespaceResolver)

将元素内容作为请求类型读取。Reads the element content as the requested type.

public:
 virtual System::Object ^ ReadElementContentAs(Type ^ returnType, System::Xml::IXmlNamespaceResolver ^ namespaceResolver);
public virtual object ReadElementContentAs (Type returnType, System.Xml.IXmlNamespaceResolver namespaceResolver);
abstract member ReadElementContentAs : Type * System.Xml.IXmlNamespaceResolver -> obj
override this.ReadElementContentAs : Type * System.Xml.IXmlNamespaceResolver -> obj
Public Overridable Function ReadElementContentAs (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.

返回

转换为请求类型的对象的元素内容。The element content converted to the requested typed 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.

读取 Decimal.MaxValueRead Decimal.MaxValue.

示例

下面的示例使用ReadElementContentAs方法来读取的内容date节点。The following example uses the ReadElementContentAs method to read the content of the date node.

using (XmlReader reader = XmlReader.Create("dataFile.xml")) {
     reader.ReadToFollowing("date");
     DateTime date = (DateTime) reader.ReadElementContentAs(typeof(System.DateTime), null);

     // 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 = CType(reader.ReadElementContentAs(GetType(System.DateTime), Nothing), DateTime)
            
  ' 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.

有关详细信息,请参阅备注部分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.

此方法的异步版本,请参阅ReadElementContentAsAsyncFor the asynchronous version of this method, see ReadElementContentAsAsync.

ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) ReadElementContentAs(Type, IXmlNamespaceResolver, String, String)

检查指定的本地名称和命名空间 URI 与当前元素的本地名称和命名空间 URI 是否匹配,然后将元素内容作为请求类型读取。Checks that the specified local name and namespace URI matches that of the current element, then reads the element content as the requested type.

public:
 virtual System::Object ^ ReadElementContentAs(Type ^ returnType, System::Xml::IXmlNamespaceResolver ^ namespaceResolver, System::String ^ localName, System::String ^ namespaceURI);
public virtual object ReadElementContentAs (Type returnType, System.Xml.IXmlNamespaceResolver namespaceResolver, string localName, string namespaceURI);
abstract member ReadElementContentAs : Type * System.Xml.IXmlNamespaceResolver * string * string -> obj
override this.ReadElementContentAs : Type * System.Xml.IXmlNamespaceResolver * string * string -> obj
Public Overridable Function ReadElementContentAs (returnType As Type, namespaceResolver As IXmlNamespaceResolver, localName As String, namespaceURI As String) 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.

localName
String String String String

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

namespaceURI
String String String String

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

返回

转换为请求类型的对象的元素内容。The element content converted to the requested typed 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.

读取 Decimal.MaxValueRead Decimal.MaxValue.

注解

此方法读取开始标记,该元素的内容,并移过去的结束元素标记读取器。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.

有关详细信息,请参阅备注部分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.

适用于