XmlReader.ReadElementContentAsObject 方法

定义

读取当前元素,并将内容返回为 .Object

重载

名称 说明
ReadElementContentAsObject()

读取当前元素,并将内容返回为 .Object

ReadElementContentAsObject(String, String)

检查指定的本地名称和命名空间 URI 是否与当前元素的本地名称和命名空间 URI 匹配,然后读取当前元素并将内容作为一个 Object返回。

ReadElementContentAsObject()

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs

读取当前元素,并将内容返回为 .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) 对象。 该 ValueType 属性确定适当的 CLR 类型。 如果内容类型为列表类型,此方法将返回相应类型的装箱对象数组。

例外

XmlReader 元素不定位在元素上。

-或-

在上一个异步操作完成之前调用了一个 XmlReader 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。

当前元素包含子元素。

-或-

元素内容无法转换为请求的类型

使用参数调用 null 该方法。

示例

以下示例使用该方法读取节点的内容 price 。 读取器使用架构中的信息将内容映射到正确的数据类型。

// 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)

此示例使用以下两个文件作为输入。

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>

注解

此方法读取开始标记、元素的内容,并将读取器移到结束元素标记之后。 它扩展实体并忽略处理指令和注释。 元素只能包含简单内容。 也就是说,它不能有子元素。

有关详细信息,请参阅参考页的 XmlReader “备注”部分和 W3C XML 架构第 2 部分:数据类型 建议。

有关此方法的异步版本,请参阅 ReadElementContentAsObjectAsync

适用于

ReadElementContentAsObject(String, String)

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs

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

元素的本地名称。

namespaceURI
String

元素的命名空间 URI。

返回

最合适类型的装箱公共语言运行时 (CLR) 对象。 该 ValueType 属性确定适当的 CLR 类型。 如果内容类型为列表类型,此方法将返回相应类型的装箱对象数组。

例外

XmlReader 元素不定位在元素上。

-或-

在上一个异步操作完成之前调用了一个 XmlReader 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。

当前元素包含子元素。

-或-

元素内容无法转换为请求的类型。

使用参数调用 null 该方法。

指定的本地名称和命名空间 URI 与正在读取的当前元素的 URI 不匹配。

注解

此方法读取开始标记、元素的内容,并将读取器移到结束元素标记之后。 它扩展实体并忽略处理指令和注释。 元素只能包含简单内容。 也就是说,它不能有子元素。

有关详细信息,请参阅参考页的 XmlReader “备注”部分和 W3C XML 架构第 2 部分:数据类型 建议。

适用于