Export (0) Print
Expand All
Expand Minimize

XmlReader.ReadContentAs Method

Reads the content as an object of the type specified.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

'Declaration
Public Overridable Function ReadContentAs ( _
	returnType As Type, _
	namespaceResolver As IXmlNamespaceResolver _
) As Object

Parameters

returnType
Type: System.Type

The type of the value to be returned.

Note   With the release of the .NET Framework 3.5, the value of the returnType parameter can now be the DateTimeOffset type.

namespaceResolver
Type: System.Xml.IXmlNamespaceResolver

An IXmlNamespaceResolver object that is used to resolve any namespace prefixes related to type conversion. For example, this can be used when converting an XmlQualifiedName object to an xs:string.

This value can be Nothing.

Return Value

Type: System.Object
The concatenated text content or attribute value converted to the requested type.

ExceptionCondition
FormatException

The content is not in the correct format for the target type.

InvalidCastException

The attempted cast is not valid.

ArgumentNullException

The returnType value is Nothing.

InvalidOperationException

The current node is not a supported node type. See the table below for details.

OverflowException

Read Decimal.MaxValue.

InvalidOperationException

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.”

This method reads the text content at the current reader position and converts 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.

This method is used to read, convert if necessary, and return atomic value items from the current node content. If the input type is a valid mappings for the type of the current node then an instance of the target type containing the value of the current node is returned. See the Remarks section in the XmlReader reference page for a list of the default mappings.

For example, if you had the following XML text:

<elem>123 <!-- comment --> <?pi my_text?> 456 <?pi another_pi?></elem>

If the data is typed and a string array is supplied to the ReadContentAs method call, then the integer values are converted from strings according to the list of valid CLR type mappings.

If the data is untyped and a string array is supplied to the ReadContentAs method call, then the content is parsed into separate strings. An array containing two strings is returned with the values "123" and "456". The spaces are not preserved from the content.

In general when reading untyped data the content is parsed according to the supplied type. For example, if an integer array is supplied to the ReadContentAs method call then the string is parsed into an array of integers {123,456}.

In the following example the XML text is not separated by spaces

<elem>123<!-- comment --><?pi my_text?>456789<?pi another_pi?></elem>

If the content is untyped and a string array is supplied to the ReadContentAs method call then an array containing one concatenated string is returned with the value "123456789".

The following table describes how this method treats 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 calling 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.

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 ReadContentAsAsync.

The following example uses the ReadContentAs method to return the contents of the colors element into an array of string objects.

Using reader As XmlReader = XmlReader.Create("dataFile_2.xml")

  reader.ReadToDescendant("item")

  reader.MoveToAttribute("colors")
  Dim colors As String() = CType(reader.ReadContentAs(GetType(String()), Nothing), String())
  Dim color As String 
  For Each color In  colors
    Console.WriteLine("Colors: {0}", color)
  Next color

End Using

The example uses the dataFile_2.xml file as input.

<root>
  <item sale-item='true' productID='123456' colors='blue green black'>
    <price>9.95</price>
  </item>
  <item sale-item='false' productID='124390'>
    <price>5.95</price>
  </item>
  <item sale-item='true' productID='53298'>
    <price>12.95</price>
  </item>
</root>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft