Parsing XML with the XmlReader
XML Parser in C# and VB.Net

XML is a self describing language and it gives the data as well as the rules to extract what data it contains. Parsing (reading) an XML file means that we are reading the information embedded in XML tags in an XML file. You can parse or read an XML Document in many ways. In this chapter you can see some of the easiest ways to read the XML file.
Parse XML with XmlReader
This following program describes how to use the XmlReader class to parse an XML string in. The XmlReader is a faster and less memory consuming alternative. It lets you run through the XML string one element at a time, while allowing you to look at the value, and then moves on to the next XML element.
C# XmlReader example
Sample XML string1100 Windows 7 2000
Read XML with XmlReader in C#
using System; using System.Data; using System.Xml; using System.IO; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { String xmlNode = ""; XmlReader xReader = XmlReader.Create(new StringReader(xmlNode)); while (xReader.Read()) { switch (xReader.NodeType) { case XmlNodeType.Element: listBox1.Items.Add("<" + xReader.Name + ">"); break; case XmlNodeType.Text: listBox1.Items.Add(xReader.Value); break; case XmlNodeType.EndElement: listBox1.Items.Add("" + xReader.Name + ">"); break; } } } } } 1100 Windows 7 2000
C# XML Parser
Output:

Parse XML with XmlReader in VB.Net
This following program describes how to use the XmlReader class to parse an XML string in VB.Net.
VB.Net XmlReader example
Sample XML string1100 Windows 7 2000
Read XML with XmlReader in VB.Net
Imports System.Xml Imports System.IO Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xmlNode As String = "" & _ " " & _ " " & _ " " & _ " 1100 " & _ " Windows 7 " & _ " 2000 " Dim xReader As XmlReader = XmlReader.Create(New StringReader(xmlNode)) While xReader.Read() Select Case xReader.NodeType Case XmlNodeType.Element ListBox1.Items.Add("<" + xReader.Name & ">") Exit Select Case XmlNodeType.Text ListBox1.Items.Add(xReader.Value) Exit Select Case XmlNodeType.EndElement ListBox1.Items.Add("" + xReader.Name & ">") Exit Select End Select End While End Sub End Class
VB.Net XML Parser
NEXT.....Parse XML - XmlTextReader