Click here to Skip to main content
Licence CPOL
First Posted 30 Oct 2008
Views 18,196
Bookmarked 12 times

Using an XML file as One Table Database

By | 21 Oct 2009 | Article
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)

About the Author

Wolfeye

Web Developer

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberRodrigo Lourenço8:22 25 Jun '10  
GeneralMy vote of 1 PinmemberEdmilson S. Teixeira9:48 12 Mar '10  
GeneralDisplaying XML data in a tabular format in a form PinmemberSam Hobbs19:57 22 Oct '09  
GeneralMisleading becasue the form is not a form PinmemberSam Hobbs12:48 21 Oct '09  
GeneralThe properties of the DataSet PinmemberSam Hobbs15:36 20 Oct '09  
GeneralRe: The properties of the DataSet PinmemberSam Hobbs12:25 21 Oct '09  
GeneralAnd... PinmemberPIEBALDconsult7:11 30 Oct '08  
GeneralWell... PinmvpDave Kreskowiak6:00 30 Oct '08  
GeneralRe: Well... Pinmemberthund3rstruck7:53 30 Oct '08  
GeneralRe: Well... PinmemberWolfeye7:57 30 Oct '08  
GeneralRe: Well... PinmemberSuperShade9:08 30 Oct '08  
GeneralRe: Well... PinmemberWolfeye9:28 30 Oct '08  
GeneralRe: Well... PinmvpDave Kreskowiak15:50 30 Oct '08  
GeneralRe: Well... PinmemberSam Hobbs15:32 20 Oct '09  
GeneralRe: Well... PinmvpDave Kreskowiak18:27 20 Oct '09  
GeneralRe: Well... PinmemberSam Hobbs18:42 20 Oct '09  
GeneralRe: Well... PinmvpDave Kreskowiak2:21 21 Oct '09  
GeneralRe: Well... PinmemberSam Hobbs5:26 21 Oct '09  
GeneralRe: Well... PinmvpDave Kreskowiak11:15 21 Oct '09  
GeneralRe: Well... PinmemberSam Hobbs12:30 21 Oct '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 Oct 2009
Article Copyright 2008 by Wolfeye
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid