Breyta

Deila með


XmlReader.ReadElementContentAsObject Method

Definition

Reads the current element and returns the contents as an Object.

Overloads

ReadElementContentAsObject()

Reads the current element and returns the contents as an Object.

ReadElementContentAsObject(String, String)

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

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

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

Returns

A boxed common language runtime (CLR) object of the most appropriate type. 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.

Exceptions

The XmlReader is not positioned on an element.

-or-

An XmlReader method was called before a previous asynchronous operation finished. 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

The method is called with null arguments.

Examples

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>

Remarks

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.

For more information, see the Remarks section of the XmlReader reference page and the W3C XML Schema Part 2: Datatypes recommendation.

For the asynchronous version of this method, see ReadElementContentAsObjectAsync.

Applies to

ReadElementContentAsObject(String, String)

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

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

Parameters

localName
String

The local name of the element.

namespaceURI
String

The namespace URI of the element.

Returns

A boxed common language runtime (CLR) object of the most appropriate type. 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.

Exceptions

The XmlReader is not positioned on an element.

-or-

An XmlReader method was called before a previous asynchronous operation finished. 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.

The method is called with null arguments.

The specified local name and namespace URI do not match that of the current element being read.

Remarks

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.

For more information, see the Remarks section of the XmlReader reference page and the W3C XML Schema Part 2: Datatypes recommendation.

Applies to