Parsing XML with the XmlDocument

XML Parser in C# and VB.Net

Read XML with XmlDocument in VB.Net

XmlDocument Class represents an XML document. XmlDocument reads the entire XML content into memory and then allow you to navigate back and forward in it or even query the XML document using the XPath technology.

Using XPath with the XmlDocument class

Parse XML with XmlDocument in VB.Net

XmlNodeList Class Represents an ordered collection of nodes. In order to find nodes in an XML file you can use XPath expressions. Method XmlNode.SelectNodes returns a list of nodes selected by the XPath string.


doc.DocumentElement.SelectNodes("/Store/Product");
Reading XML with the XmlDocument class

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

Parse XML with XmlDocument

The following program describes how to use the XmlDocument class to parse an XML document in C#.

C# XmlDocument example
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)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("d:\\product.xml");
            XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Store/Product");
            string product_id = "", product_name = "", product_price="";
            foreach (XmlNode node in nodes)
            {
                product_id = node.SelectSingleNode("Product_id").InnerText;
                product_name = node.SelectSingleNode("Product_name").InnerText;
                product_price = node.SelectSingleNode("Product_price").InnerText;
                MessageBox.Show(product_id + " " + product_name + " " + product_price);
            }
        }
    }
}
VB.Net XmlDocument example VB.Net XmlDocument example

This following program describes how to use the XmlDocument class to parse an XML document in VB.Net.

Imports System.Xml
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim doc As New XmlDocument()
        doc.Load("d:\product.xml")
        Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/Store/Product")
        Dim product_id As String = "", product_name As String = "", product_price As String = ""
        For Each node As XmlNode In nodes
            product_id = node.SelectSingleNode("Product_id").InnerText
            product_name = node.SelectSingleNode("Product_name").InnerText
            product_price = node.SelectSingleNode("Product_price").InnerText
            MessageBox.Show(product_id & " " & product_name & " " & product_price)
        Next
    End Sub
End Class


NEXT.....File.Exists Method