XmlReader.ReadElementContentAsObject XmlReader.ReadElementContentAsObject XmlReader.ReadElementContentAsObject XmlReader.ReadElementContentAsObject Method

定义

读取当前元素并将内容作为 Object 返回。Reads the current element and returns the contents as an Object.

重载

ReadElementContentAsObject() ReadElementContentAsObject() ReadElementContentAsObject() ReadElementContentAsObject()

读取当前元素并将内容作为 Object 返回。Reads the current element and returns the contents as an Object.

ReadElementContentAsObject(String, String) ReadElementContentAsObject(String, String) ReadElementContentAsObject(String, String) ReadElementContentAsObject(String, String)

检查指定的本地名称和命名空间 URI 与当前元素的本地名称和命名空间 URI 是否匹配,然后读取当前元素,并将内容作为 Object 返回。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 an Object.

ReadElementContentAsObject() ReadElementContentAsObject() ReadElementContentAsObject() ReadElementContentAsObject()

读取当前元素并将内容作为 Object 返回。Reads the current element and returns the contents as an Object.

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

返回

一个最适当类型的装箱的公共语言运行时 (CLR) 对象。A boxed common language runtime (CLR) object of the most appropriate type. ValueType 属性确定了适当的 CLR 类型。The ValueType property determines the appropriate CLR type. 如果将内容类型化为列表类型,则此方法返回一个适当类型的装箱对象的数组。If the content is typed as a list type, this method returns an array of boxed objects of the appropriate type.

异常

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.

示例

下面的示例使用方法来读取的内容price节点。The following example uses the method to read the content of the price node. 读取器使用的架构中的信息将内容映射到正确的数据类型。The reader uses the information in the schema to map the content to the correct data type.

// Create a validating reader.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("urn:items", "item.xsd");	
 XmlReader reader = XmlReader.Create("item.xml", settings); 

// Get the CLR type of the price element. 
reader.ReadToFollowing("price");
Console.WriteLine(reader.ValueType);

// Return the value of the price element as Decimal object.
Decimal price = (Decimal) reader.ReadElementContentAsObject();

// Add 2.50 to the price.
price = Decimal.Add(price, 2.50m);

' Create a validating reader.
Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas.Add("urn:items", "item.xsd")
Dim reader As XmlReader = XmlReader.Create("item.xml", settings)
      
' Get the CLR type of the price element. 
reader.ReadToFollowing("price")
Console.WriteLine(reader.ValueType)
      
' Return the value of the price element as Decimal object.
Dim price As [Decimal] = CType(reader.ReadElementContentAsObject(), [Decimal])
      
' Add 2.50 to the price.
price = [Decimal].Add(price, 2.5D)

该示例使用以下两个文件The example uses the following two files as input.

item.xml

<item xmlns="urn:items" productID='123098'>
 <name>hammer</name>
 <price>9.95</price>
 <supplierID>1929</supplierID>
</item>

item.xsd

<?xml version="1.0"?>
<xs:schema xmlns:tns="urn:items" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:items" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string" />
        <xs:element name="price" type="xs:decimal" />
        <xs:element name="supplierID" type="xs:unsignedShort" />
      </xs:sequence>
      <xs:attribute name="productID" type="xs:unsignedInt" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>

注解

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

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

ReadElementContentAsObject(String, String) ReadElementContentAsObject(String, String) ReadElementContentAsObject(String, String) ReadElementContentAsObject(String, String)

检查指定的本地名称和命名空间 URI 与当前元素的本地名称和命名空间 URI 是否匹配,然后读取当前元素,并将内容作为 Object 返回。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 an Object.

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

参数

localName
String String String String

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

namespaceURI
String String String String

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

返回

一个最适当类型的装箱的公共语言运行时 (CLR) 对象。A boxed common language runtime (CLR) object of the most appropriate type. ValueType 属性确定了适当的 CLR 类型。The ValueType property determines the appropriate CLR type. 如果将内容类型化为列表类型,则此方法返回一个适当类型的装箱对象的数组。If the content is typed as a list type, this method returns an array of boxed objects of the appropriate type.

异常

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.

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

适用于