Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

Using an XML file as One Table Database

Rate me:
Please Sign up or sign in to vote.
1.05/5 (10 votes)
21 Oct 2009CPOL2 min read 39K   13   20
How to use a DataSet object to read and write XML

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I've been a hack programmer for years, working with Delphi, C++, VB.NET, and C#. I know enough to be very dangerous but not enough to be promotable.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Rodrigo Lourenço25-Jun-10 8:22
Rodrigo Lourenço25-Jun-10 8:22 
GeneralMy vote of 1 Pin
Edmilson S. Teixeira12-Mar-10 9:48
Edmilson S. Teixeira12-Mar-10 9:48 
GeneralDisplaying XML data in a tabular format in a form Pin
Sam Hobbs22-Oct-09 19:57
Sam Hobbs22-Oct-09 19:57 
GeneralMisleading becasue the form is not a form Pin
Sam Hobbs21-Oct-09 12:48
Sam Hobbs21-Oct-09 12:48 
This article is very misleading in that it creates a form application that does not use the form.
GeneralThe properties of the DataSet Pin
Sam Hobbs20-Oct-09 15:36
Sam Hobbs20-Oct-09 15:36 
GeneralRe: The properties of the DataSet Pin
Sam Hobbs21-Oct-09 12:25
Sam Hobbs21-Oct-09 12:25 
GeneralAnd... Pin
PIEBALDconsult30-Oct-08 7:11
mvePIEBALDconsult30-Oct-08 7:11 
GeneralWell... Pin
Dave Kreskowiak30-Oct-08 6:00
mveDave Kreskowiak30-Oct-08 6:00 
GeneralRe: Well... Pin
thund3rstruck30-Oct-08 7:53
thund3rstruck30-Oct-08 7:53 
GeneralRe: Well... Pin
Wolfeye30-Oct-08 7:57
Wolfeye30-Oct-08 7:57 
GeneralRe: Well... Pin
SuperShade30-Oct-08 9:08
SuperShade30-Oct-08 9:08 
GeneralRe: Well... Pin
Wolfeye30-Oct-08 9:28
Wolfeye30-Oct-08 9:28 
GeneralRe: Well... Pin
Dave Kreskowiak30-Oct-08 15:50
mveDave Kreskowiak30-Oct-08 15:50 
GeneralRe: Well... Pin
Sam Hobbs20-Oct-09 15:32
Sam Hobbs20-Oct-09 15:32 
GeneralRe: Well... Pin
Dave Kreskowiak20-Oct-09 18:27
mveDave Kreskowiak20-Oct-09 18:27 
GeneralRe: Well... Pin
Sam Hobbs20-Oct-09 18:42
Sam Hobbs20-Oct-09 18:42 
GeneralRe: Well... Pin
Dave Kreskowiak21-Oct-09 2:21
mveDave Kreskowiak21-Oct-09 2:21 
GeneralRe: Well... Pin
Sam Hobbs21-Oct-09 5:26
Sam Hobbs21-Oct-09 5:26 
GeneralRe: Well... Pin
Dave Kreskowiak21-Oct-09 11:15
mveDave Kreskowiak21-Oct-09 11:15 
GeneralRe: Well... Pin
Sam Hobbs21-Oct-09 12:30
Sam Hobbs21-Oct-09 12:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.