How to: Parse XML with XmlReader

Microsoft Silverlight will reach end of support after October 2021. Learn more.

This topic provides two examples of how to use XmlReader to parse an XML stream in the Microsoft .NET Framework for Silverlight.

For more examples of how to parse XML with XmlReader, see the following topics in the .NET Framework documentation:

To configure a Silverlight Visual Studio project to run this example

  1. Modify your page.xaml file so that it includes the following TextBlock element:

    <TextBlock x:Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/>
    
  2. In the page.xaml.cs (page.xaml.vb in Visual Basic) source file for your application, add the following using statements (Imports in Visual Basic):

    Imports System.Xml
    Imports System.IO
    Imports System.Text
    
    using System.Xml;
    using System.IO;
    using System.Text;
    

Example

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

Dim output As StringBuilder = New StringBuilder()

Dim xmlString As String = "<?xml version='1.0'?>" & _
                "<!-- This is a sample XML document -->" & _
                "<Items>" & _
                  "<Item>test with a child element <more/> stuff</Item>" & _
                "</Items>"
' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
    Dim ws As XmlWriterSettings = New XmlWriterSettings()
    ws.Indent = True
    Using writer As XmlWriter = XmlWriter.Create(output, ws)

        ' Parse the file and display each of the nodes.
        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    writer.WriteStartElement(reader.Name)
                Case XmlNodeType.Text
                    writer.WriteString(reader.Value)
                Case XmlNodeType.XmlDeclaration
                Case XmlNodeType.ProcessingInstruction
                    writer.WriteProcessingInstruction(reader.Name, reader.Value)
                Case XmlNodeType.Comment
                    writer.WriteComment(reader.Value)
                Case XmlNodeType.EndElement
                    writer.WriteFullEndElement()
            End Select
        End While
    End Using
End Using
OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();

String xmlString =
        @"<?xml version='1.0'?>
        <!-- This is a sample XML document -->
        <Items>
          <Item>test with a child element <more/> stuff</Item>
        </Items>";
// 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:
                    writer.WriteStartElement(reader.Name);
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
            }
        }

    }
}
OutputTextBlock.Text = output.ToString();

The following example uses the XmlReader methods to read the content of elements and attributes.

Dim output As StringBuilder = New StringBuilder()

Dim xmlString As String = _
    "<bookstore>" & _
            "<book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>" & _
                "<title>The Autobiography of Benjamin Franklin</title>" & _
                "<author>" & _
                    "<first-name>Benjamin</first-name>" & _
                    "<last-name>Franklin</last-name>" & _
                "</author> " & _
                "<price>8.99</price>" & _
            "</book>" & _
        "</bookstore>"

' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))

    reader.ReadToFollowing("book")
    reader.MoveToFirstAttribute()
    Dim genre As String = reader.Value
    output.AppendLine("The genre value: " + genre)

    reader.ReadToFollowing("title")
    output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString())
End Using

OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();

String xmlString =
    @"<bookstore>
        <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>
            <title>The Autobiography of Benjamin Franklin</title>
            <author>
                <first-name>Benjamin</first-name>
                <last-name>Franklin</last-name>
            </author>
            <price>8.99</price>
        </book>
    </bookstore>";

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    reader.ReadToFollowing("book");
    reader.MoveToFirstAttribute();
    string genre = reader.Value;
    output.AppendLine("The genre value: " + genre);

    reader.ReadToFollowing("title");
    output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString());
}

OutputTextBlock.Text = output.ToString();