Share via


Reading Typed Data

The XmlReader class permits callers to read XML data and return values as simple-typed common language runtime (CLR) values rather than strings. Using the methods provided, users can get the values using system types rather than strings. These new methods enable users to get values in the representation that is most appropriate for the coding job without having to manually perform value conversions and parsing.

If the XML data does not have any schema or data type information associated with it, the XmlReader class attempts to convert the node value to the CLR type specified. For example, if you call the ReadContentAsBoolean method, the reader attempts to covert the text to a Boolean object.

The XmlReader class uses the rules defined by the W3C XML Schema Part 2: Datatypes recommendation.

Typed Content

The ReadContentAsBoolean, ReadContentAsDateTime, ReadContentAsDouble, ReadContentAsLong, ReadContentAsInt, and ReadContentAsString methods are used to return a specific CLR object. These methods read the text content at the current reader position and convert it to the requested return type. Text, white space, significant white space and CDATA sections are concatenated. Comments and processing instructions are skipped and entity references are automatically resolved.

The ReadContentAs method is used to read the text content and return an object of the type specified.

The ReadContentAsObject method returns a boxed CLR of the most appropriate type, specified by the ValueType property. If the content is not typed, the reader returns the content as a string.

After calling any of the ReadContentAs methods, the reader is positioned on the next Element or EndElement node.

The following table describes how the ReadContentAs methods treat each node type.

XmlNodeType

Return value

Reader behavior

Text

CDATA

Whitespace

SignificantWhitespace

EntityReference

EndEntity

Concatenated content of text, CDATA, white space and significant white space nodes converted to the requested type.

Moves to the next start element or end element tag. Entity references are automatically expanded.

Attribute

Same as XmlConvert.ToXXX on the attribute value.

The reader remains in the current position.

Comment

ProcessingInstruction

Ignores the processing instruction (PI) or comment and reads the concatenated text content that follows the PI or comment.

Moves to the next start element or end element tag. Entity references are automatically expanded.

EndElement

An empty string.

The reader remains in the current position.

Element

XmlDeclaration

None

Document

DocumentType

Notation

Entity

DocumentFragment

An InvalidOperationException is thrown.

Undefined, although typically the reader remains in the current position.

Typed Element Content

The XmlReader class has methods designed specifically for reading typed element content. These methods can only be called on element node types. These methods cannot be used on elements that contain child elements or mixed content. When called, the XmlReader object reads the start tag, reads the element content, and then moves past the end element tag. Processing instructions and comments are ignored and entities are expanded.

The ReadElementContentAsBoolean, ReadElementContentAsDateTime, ReadElementContentAsDouble, ReadElementContentAsLong, ReadElementContentAsInt, and ReadElementContentAsString methods read element content and return a specific CLR object.

The ReadElementContentAs method is used to read element content and return an object of the type specified.

The ReadElementContentAsObject method returns a boxed CLR of the most appropriate type, specified by the ValueType property. The Mapping XML Data Types to CLR Types topic describes how the XmlReader class determines the most appropriate CLR type. If the content is not typed, the reader returns the content as a string.

See Also

Concepts

Reading XML with the XmlReader

How to: Read Typed Data Using a Schema Mapping

How to: Read Typed Data with No Schema Mapping

Mapping XML Data Types to CLR Types