Friday, October 19, 2012

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

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.
01.private void btnCreateXML_Click(object sender, EventArgs e)
02.{
03.try
04.{
05.//Create a datatable to store XML data
06.DataTable DT = new DataTable();
07.DT.Columns.Add("FirstName");
08.DT.Columns.Add("LastName");
09.DT.Columns.Add("Gender");
10.DT.Columns.Add("Age");
11.DT.Rows.Add(new object[] { "Nick", "Solimine", "Male", 22 });
12.DT.Rows.Add(new object[] { "Mark", "Taylor", "Male", 32 });
13.DT.Rows.Add(new object[] { "Alice", "Warden", "Female", 20 });
14. 
15.//Create a dataset
16.DS = new DataSet();
17. 
18.//Add datatable to this dataset
19.DS.Tables.Add(DT);
20. 
21.//Write dataset to XML file
22.DS.WriteXml(txtXMLFilePath.Text);
23. 
24.MessageBox.Show("XML data written successfully to "+txtXMLFilePath.Text);
25.}
26.catch(Exception ex)
27.{
28.MessageBox.Show("Exception: "+ex.Message);
29.}
30.}

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.
01.private void btnReadXML_Click(object sender, EventArgs e)
02.{
03.try
04.{
05.//Initialize new Dataset
06.DS = new DataSet();
07. 
08.//Read XML data from file
09.DS.ReadXml(txtXMLFilePath.Text);
10. 
11.//Fill grid with XML data
12.dataGridView1.DataSource = DS.Tables[0];
13.dataGridView1.Refresh();
14. 
15.MessageBox.Show("XML data read successful");
16.}
17.catch(Exception ex)
18.{
19.MessageBox.Show("Exception: " + ex.Message);
20.}
21.}

Save XML file Button click event

This event save XML data from Grid to file specified in txtXMLFilePath text box.
01.private void btnSaveXML_Click(object sender, EventArgs e)
02.{
03.try
04.{
05.//Write dataset to XML file
06.DS.WriteXml(txtXMLFilePath.Text);
07.MessageBox.Show("XML data saved successfully to " + txtXMLFilePath.Text);
08.}
09.catch (Exception ex)
10.{
11.MessageBox.Show("Exception: " + ex.Message);
12.}
13.}

Clear Grid Button click event

This event clears data from Grid.
1.private void btnClearGrid_Click(object sender, EventArgs e)
2.{
3.//Clear Grid by setting it's datasource property to null.
4.dataGridView1.DataSource = null;
5.}

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.

No comments:

Post a Comment