Parsing XML with the XmlTextReader

XML, being a self-descriptive language, not only contains data but also provides the necessary rules for parsing and extracting the data it encapsulates. Parsing, which involves reading an XML file, entails the extraction of information found within the XML tags present in the document. There are multiple methods to parse or read an XML Document, and this chapter illustrates one of the simplest and most straightforward approaches for reading XML files.

XML Parser in C# and VB.Net

Parse XML with XmlReader C# VB.Net

Parsing an XML file, developers gain access to the valuable data encapsulated within the XML tags while adhering to the defined rules and structure specified within the XML document. The parsing process facilitates efficient extraction and processing of relevant data, enabling seamless integration and utilization of the information stored in XML files.

Throughout this chapter, readers are introduced to an easily implementable method for reading XML files, providing a user-friendly solution that enhances comprehension and simplifies the parsing process. By utilizing this approach, developers can effectively interact with XML data, making it a valuable resource for various software applications and data manipulation tasks.

Parse XML with XmlTextReader in C# and VB.Net

Parse XML with XmlTextReader in C# VB.Net

The XmlTextReader Class offers a forward-only, read-only approach to accessing an XML data stream. The presented program showcases the utilization of the XmlTextReader class to read XML (Extensible Markup Language) data from a file. By employing XmlTextReader, developers can directly parse and tokenize XML content, adhering to the XML 1.0 standard.

This program stands out for providing swift and tokenized stream access to XML, eliminating the need for an object model like the XML DOM (Document Object Model). Instead of loading the entire XML document into memory, XmlTextReader offers an efficient way to read and process XML data sequentially. This characteristic makes it well-suited for handling large XML files, conserving memory resources and reducing processing overhead.

How to read XML file using XmlTextReader

XML File content
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Store> <Product> <Product_id>1</Product_id> <Product_name>Product 1</Product_name> <Product_price>1000</Product_price> </Product> <Product> <Product_id>2</Product_id> <Product_name>Product 2</Product_name> <Product_price>2000</Product_price> </Product> <Product> <Product_id>3</Product_id> <Product_name>Product 3</Product_name> <Product_price>3000</Product_price> </Product> <Product> <Product_id>4</Product_id> <Product_name>Product 4</Product_name> <Product_price>4000</Product_price> </Product> </Store>

Click here to download Product.xml

The XmlTextReader's forward-only nature means that it moves linearly through the XML data, permitting access to the elements in the order they appear. As it parses the XML, it generates events for different XML constructs, allowing developers to respond to these events and extract data as needed.

Read XML with XmlTextReader in C#

using System; using System.Data; using System.Xml; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { XmlTextReader xmlReader = new XmlTextReader("d:\\product.xml"); while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: listBox1.Items.Add("<" + xmlReader.Name + ">"); break; case XmlNodeType.Text: listBox1.Items.Add(xmlReader.Value); break; case XmlNodeType.EndElement: listBox1.Items.Add("</" + xmlReader.Name + ">"); break; } } } } }

C# XML Parser

Output:
XML Parser C# VB.Net

Read XML with XmlTextReader in VB.Net

Read XML with XmlTextReader in VB.Net

The following VB.Net program parse (read) the XML file (Product.xml) using XmlTextReader and display it in a ListBox.

Imports System.Xml Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xmlReader As New XmlTextReader("d:\product.xml") While xmlReader.Read() Select Case xmlReader.NodeType Case XmlNodeType.Element listBox1.Items.Add("<" + xmlReader.Name & ">") Exit Select Case XmlNodeType.Text listBox1.Items.Add(xmlReader.Value) Exit Select Case XmlNodeType.EndElement listBox1.Items.Add("</" + xmlReader.Name & ">") Exit Select End Select End While End Sub End Class

Conclusion

The XmlTextReader Class is a powerful tool for reading XML data efficiently, particularly when dealing with sizeable XML files, as it offers direct parsing and tokenizing capabilities. By using XmlTextReader, developers can access XML data incrementally, enhancing performance and reducing memory usage compared to object model-based approaches like XML DOM.