How To Read XML Data into DataSet by Using C# .NET

Sunday, June 3, 2012

During my past experience i have worked on complex xml logics and learned a lot of depth knowledge that how xml data can be integrated into Dot Net application. So, i want to share a very basic knowledge about xml integration.

The first step to integrate xml is to Read XML file and get data from it, so that the existing application can work on that data. I am sharing a basic tutorial which will guide you to start working on xml by showing you how to read data from it.


I tried to create a simple User Interface. (isn't it) :)




By clicking on Browse button, a file dialogue will open just select an xml file (Note: this dialogue will only allow you to select *.xml files) and i will show you that how this useful feature can be utilised in our application to filter files in OpenFileDialog control.


When you select an xml file, this simple application will open this xml into the below Data Grid View Control (Like this):




The very simple code behind of this application is following:




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace XmlViewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                ShowXmlData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void ShowXmlData()
        {
            //IT WILL SHOW THE DIALOG BOX FOR BROWSING XML FILE
            //AND SAVING THEN SHOW IT TO TEXTBOX
            openFileDialog1.ShowDialog();
            txtFilePath.Text = openFileDialog1.FileName;


            if (ValidateFileName(txtFilePath.Text))
            {
                //IT WILL READ DATA FROM SELECTED XML
                //AND SHOW IT TO DATA GRID VIEW CONTROL
                DataSet ds = new DataSet();
                ds.ReadXml(txtFilePath.Text);
                dgvXmlData.DataSource = ds.Tables[0];
            }
            else
            {
                throw new Exception("Invalid Data");
            }
        }


        private bool ValidateFileName(string fileName)
        {
            //YOU CAN WRITE YOUR OWN LOGIC HERE
            //TO FILTER FURTHER


            if (fileName != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

How to restrict OpenFileDialog for *.xml files:



  1. Select OpenFileDialog from Form and View the Properties.
  2. Select "Filter" field.
  3. Type "xml files|*.xml".
You can use this option to filter any type of file e.g. (*.txt, *.doc, *.wav etc.).

Hope this post if useful for you, If you have any question or suggestion then please contact me.