Read Write XML data using ADO.Net dataset and C#

Introduction

In this article, I will show you how you can write simple XML files using C# and ADO.Net dataset. You can use this technique to save data into an XML file instead of a database. This can become very handy if you need to work with a very small amount of data and you don't want to use a database.

Read/Write XML file

Download Sample

Download sample from above link and open it with Visual Studio. I have used Visual Studio 2008 for this sample. You may need to convert this project if you are using higher version of Visual Studio. When you run this sample, you would see Create XML File button on top of the form. Click on it to create a new XML file on C: drive. XML file path is mentioned in text box. You can change this path if you want. Then you can click on Read XML file. This will read XML file that program has just created. This will show you XML data in a grid view. You can modify data in Grid View and click on Save XML file button to save it back to C: drive. To check if modified data was saved, you can click on Clear Grid button and then click Read XML button. Grid should display modified data.

Source explaination

Sourcecode is very easy to understand. I have added comments between code to explain what it does.

Create XML file Button click event

This event first creates a datatable DT with dummy data. Datatable DT is then added to dataset DS. Dataset has a WriteXml() method that writes data to specified path.

 
        private void btnCreateXML_Click(object sender, EventArgs e)
        {
            try
            {
                //Create a datatable to store XML data
                DataTable DT = new DataTable();
                DT.Columns.Add("FirstName");
                DT.Columns.Add("LastName");
                DT.Columns.Add("Gender");
                DT.Columns.Add("Age");
                DT.Rows.Add(new object[] { "Nick", "Solimine", "Male", 22 });
                DT.Rows.Add(new object[] { "Mark", "Taylor", "Male", 32 });
                DT.Rows.Add(new object[] { "Alice", "Warden", "Female", 20 });

                //Create a dataset
                DS = new DataSet();

                //Add datatable to this dataset
                DS.Tables.Add(DT);

                //Write dataset to XML file
                DS.WriteXml(txtXMLFilePath.Text);

                MessageBox.Show("XML data written successfully to "+txtXMLFilePath.Text);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception: "+ex.Message);
            }
        }

Read XML file Button click event

This event read XML data from file specified in txtXMLFilePath text box. Dataset has ReadXml() method that can be used to read XML data into Dataset.

        private void btnReadXML_Click(object sender, EventArgs e)
        {
            try
            {
                //Initialize new Dataset
                DS = new DataSet();

                //Read XML data from file
                DS.ReadXml(txtXMLFilePath.Text);

                //Fill grid with XML data
                dataGridView1.DataSource = DS.Tables[0];
                dataGridView1.Refresh();

                MessageBox.Show("XML data read successful");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }
        }

Save XML file Button click event

This event save XML data from Grid to file specified in txtXMLFilePath text box.

        private void btnSaveXML_Click(object sender, EventArgs e)
        {
            try
            {
                //Write dataset to XML file
                DS.WriteXml(txtXMLFilePath.Text);
                MessageBox.Show("XML data saved successfully to " + txtXMLFilePath.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }
        }

Clear Grid Button click event

This event clears data from Grid.

        private void btnClearGrid_Click(object sender, EventArgs e)
        {
            //Clear Grid by setting it's datasource property to null.
            dataGridView1.DataSource = null;
        }

Summary

It's easier to read/write data into XML file using Dataset. It has a direct methods available to do that. Structuring data is more easier using datatable and dataset. It's also possible to use XmlReader and XmlWriter classes available in System.XML namespace. But I find it little difficult to use.