Introduction
This article describes how you can use a Dataset object in C# to read and write to an XML file, using it as a simple, standalone database.
Background
There are lots of database choices out there, and they range in size from medium to big. There really isn't a tiny, miniscule database option, which is why many smaller programs use things like .csv files for data storage. XML is another option and it lets you see the hierarchical structure of your data at a glance. Microsoft's DataSet object has two methods which make using XML as a database a breeze.
Using the Code
This code is pretty simple. To use it, create a Windows Forms Project in Visual Studio (or your favorite open source IDE) and add an untyped DataSet object to it. In the properties of the DataSet, click on "Tables" and add a table. Add whatever columns you wish by clicking on the "Columns" button. You can add as many tables as you want, but for this simple demo, I have added only one called Table1, with three columns labeled Column1..3 which are default names.
Next, add a BindingSource object, a BindingNavigator object, and a DataGridView object to the form. Set the BindingSource DataSource to your DataSet object, and the DataMember to Table1. Set the BindingNavigator BindingSource to your BindingSource. Lastly, set the DataGridView DataSource to the BindingSource.
Finally, add the following code to the Form's Load and FormClosing events:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (System.Data.DataTable t in dataSet1.Tables)
{
if (System.IO.File.Exists(t.TableName + ".xml"))
{
t.ReadXml(t.TableName + ".xml");
if (System.IO.File.Exists(t.TableName + ".xsd"))
t.ReadXmlSchema(t.TableName + ".xsd");
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (System.Data.DataTable t in dataSet1.Tables)
{
t.WriteXml(t.TableName + ".xml");
t.WriteXmlSchema(t.TableName + ".xsd");
}
}
}
}
That's it! You can now read and write to the XML files as if they were a database table, using the grid control. If you had more than one table you could add a button and simply change the BindingSource DataMember.
Points of Interest
One of the nice things about using DataSet objects is that they can import any database object, not just XML. And they all can then be queried and manipulated with LINQ.
Update, 10-21-2009
I have received a lot of angry, negative feedback on this article, some claiming I am misleading people, others claiming the article simply has no merit at all. It is what it is. I challenge those who don't like this article to submit an alternative which:
Lets people use a grid to display data in a tabular format
Lets people set datatype constraints for the columns they use
Uses a technolgy without requiring any purchase of new software
Is small and completely portable and can easilly be used online and offline
That's all my article is, and was, designed to do.