Expand Minimize

XmlReader::Read Method

When overridden in a derived class, reads the next node from the stream.

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

public:
virtual bool Read() abstract

Return Value

Type: System::Boolean
true if the next node was read successfully; otherwise, false.

ExceptionCondition
XmlException

An error occurred while parsing the XML.

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

When an XmlReader is first created and initialized, there is no information available. You must call Read to read the first node. The Read method sets the state of the XML reader to initiate Initial and moves through the XML file sequentially until it reaches the end of the file, at which point the method returns a value of false.

This method requires at least four bytes from the data stream in order to begin parsing. If fewer than four bytes are returned and there is no more data in the stream, the method returns false. If there is more data in the stream, the method will block parsing until receipt of the fourth byte.

For the asynchronous version of this method, see ReadAsync.

The following example reads an XML file and displays each of the nodes.

No code example is currently available or this language may not be supported.

The sample uses the items.xml file.

<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>Test with a child element <more/> stuff</Item>
  <Item>Test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with a char entity: &#65;</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

Output:

<Item>Test with an entity: 123</Item><Item>Test with a child element <more> stuff</Item><Item>Test with a CDATA section <![CDATA[<456>]]> def</Item><Item>Test with a char entity: A</Item><!-- Fourteen chars in this element.--><Item>1234567890ABCD</Item></Items> 

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.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 Silverlight 8.1, Windows Phone Silverlight 8

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft