Read Method
Collapse the table of content
Expand the table of content

XmlReader.Read Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

public abstract bool Read()

Return Value

Type: System.Boolean
true if the next node was read successfully; false if there are no more nodes to read.


An error occurred while parsing the XML.

When an XmlReader is first created and initialized, there is no information available. You must call Read to read the first node.

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 will fail. If there is more data in the stream, the method will block parsing until receipt of the fourth byte.

The following example navigates through the stream to determine the current node type, and then uses XmlWriter to output the XmlReader content.

StringBuilder output = new StringBuilder();

String xmlString =
        @"<?xml version='1.0'?>
        <!-- This is a sample XML document -->
          <Item>test with a child element <more/> stuff</Item>
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
    XmlWriterSettings ws = new XmlWriterSettings();
    ws.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(output, ws))

        // Parse the file and display each of the nodes.
        while (reader.Read())
            switch (reader.NodeType)
                case XmlNodeType.Element:
                case XmlNodeType.Text:
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                case XmlNodeType.Comment:
                case XmlNodeType.EndElement:

OutputTextBlock.Text = output.ToString();

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft