XML Parser in C# and VB.Net

Parse XML with XmlReader C# 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

XmlReader example C# VB.Net

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 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 =
                    "<?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>";
            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("");
                        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 = "<?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>"
        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("")
                    Exit Select
            End Select
        End While
    End Sub
End Class

VB.Net XML Parser

NEXT.....Parse XML - XmlTextReader