Parse XML - XmlReader

XML is a self-descriptive language, meaning it not only holds the data it contains but also provides the rules and structure to extract that data effectively. When we parse or read an XML file, we are essentially accessing and extracting the information embedded within XML tags present in the file. There are various methods to parse or read an XML document, and this chapter presents some of the simplest and most straightforward approaches to accomplish this task.

XML Parser in C# and VB.Net

Parse XML with XmlReader C# VB.Net

Parsing an XML file, developers can access and utilize the valuable data enclosed within the XML tags, adhering to the defined rules and structure specified within the XML document. The parsing process enables applications to extract and process relevant data, thus facilitating seamless integration and utilization of information stored in XML files.

Throughout this chapter, readers will encounter a range of user-friendly techniques and methods for reading XML files, ensuring ease of implementation and quick understanding. By employing these accessible approaches, developers can effectively interact with XML data, making it a valuable resource for various software applications and data management tasks.

Parse XML with XmlReader

XmlReader example C# VB.Net

This program demonstrates the implementation of the XmlReader class to efficiently parse an XML string. The XmlReader serves as a superior option, boasting faster processing speeds and lower memory consumption. It enables developers to traverse the XML string methodically, processing one element at a time, and granting access to the element's value before seamlessly advancing to the subsequent XML element. This approach proves particularly advantageous when dealing with large XML documents or scenarios requiring efficient and resource-conscious data extraction and analysis.

C# XmlReader example

Sample XML string
<?xml version='1.0'?> <!-- This is a sample XML string --> <Product> <Product_id>1100</Product_id> <Product_name>Windows 7</Product_name> <Product_price>2000</Product_price> </Product>

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 = "<xmp><?xml version='1.0'?> <!-- This is a sample XML document --> <Product> <Product_id>1100</Product_id> <Product_name>Windows 7</Product_name> <Product_price>2000</Product_price> </Product></xmp>"; 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; } } } } }

C# XML Parser

Output:
XML Parser C# VB.Net

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 string
<?xml version='1.0'?> <!-- This is a sample XML string --> <Product> <Product_id>1100</Product_id> <Product_name>Windows 7</Product_name> <Product_price>2000</Product_price> </Product>

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 = "<xmp><?xml version='1.0'?></xmp>" & _ "<xmp><!-- This is a sample XML document --></xmp>" & _ "<xmp><Product></xmp>" & _ "<xmp><Product_id>1100</Product_id></xmp>" & _ "<xmp><Product_name>Windows 7</Product_name></xmp>" & _ "<xmp><Product_price>2000</Product_price></xmp>" & _ "<xmp></Product></xmp>" 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