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

Using .xml files to replace small databases

Rate me:
Please Sign up or sign in to vote.
4.06/5 (35 votes)
2 Sep 20031 min read 190.9K   1.6K   41   21
Let an XML file become your local small database.

Sample Image

Introduction

Many software and projects have many files to save data, like .ini, .cfg, .config... And they use any small database hourly, like .mdb. Now, using XML files can replace these files to save any data, under the .NET framework. There are 4 steps that let us complete this mission :p.

Step 1: Building an XML file.

The XML file's formatting must be familiar to you. Like this:

xmlfile

If you open this XML file in VS.NET, you can see button "Data" in the IDE's left bottom. Click the button, you can see some tables from this XML file. Like this:

Sample screenshot

Step 2: Using a DataSet to read XML file.

Create a WinForm project. Add an OpenFileDialog into the main form. We will use it to open the XML file. Like this:

C#
if(openFileDialog1.ShowDialog()==DialogResult.OK) 
{ 
  DataSet ds=new DataSet(); 
  ds.read(openFileDialog1.FileName); 
}

Step 3: Control the DataSet.

In a DataSet instance, we can do some operations like select, add, delete, and update... This is a sample method to search some data:

C#
public DataRow[] searchForm(string tables,string expression) 
{ 
  //ds is a DataSet instance. 
  DataTable dt=ds.Tables[tables]; 
  DataRow[] drs=dt.Select(expression); 
  return drs; 
}

And you can add other methods easily. Like these methods:

C#
//delete some rows from a table.
public bool deleteRows(string tables,string expression)
{
  bool result=false;
  DataTable dt=ds.Tables[tables];
  DataRow[] drs=dt.Select(expression);
  if(drs.Length>0)
  {
    for(int i=0;i<drs.Length;i++) <DRS[].LENGTH;I++)<>drs[i].Delete();
    ds.WriteXml(fileName);
    result=true;
  }
  else
  {
    result=false;
  }
  return result;
}

Step 4: Using DataSet to save data.

One statement can finish the work. Like this:

//fileName is a xml file's full name. 
if(fileName!="") 
ds.WriteXml(fileName);

Now see, the XML file can save data and we can use DataSet to operate on the data :).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Sorry,my english is very poor.My name is ShangYuan Wang,and I am working in China GuangXi nanning city,a technology company named biztig.So
my nickname is biztig,and I have an other nickname that is jam_snake. I learned pascal in GuangXi University first at 5 years ago,but my favorite language is C# now.
The image is a famous star in china,and I am her fans.

Comments and Discussions

 
Generalhello Pin
samiong727-Jan-09 2:29
samiong727-Jan-09 2:29 
GeneralFor Yantarian Pin
avodavtyan23-Oct-07 19:46
avodavtyan23-Oct-07 19:46 
GeneralInitial conditions Pin
Kujtim Hyseni1-Dec-06 3:40
Kujtim Hyseni1-Dec-06 3:40 
Generalcountrymen MM Pin
vcsharp16-Feb-04 15:25
vcsharp16-Feb-04 15:25 
GeneralGood ideas must leave something to your own imagination ... Pin
intressant@solhem.se25-Sep-03 8:22
intressant@solhem.se25-Sep-03 8:22 
GeneralComment Pin
Anonymously10-Sep-03 16:09
Anonymously10-Sep-03 16:09 
GeneralRe: Comment Pin
jam_snake10-Sep-03 16:49
jam_snake10-Sep-03 16:49 
GeneralRe: Comment Pin
Anonymously12-Sep-03 15:32
Anonymously12-Sep-03 15:32 
GeneralRe: Comment Pin
Azel Low3-Oct-03 2:40
Azel Low3-Oct-03 2:40 
Laugh | :laugh:
GeneralRe: Comment Pin
yantarian11-Nov-03 3:31
sussyantarian11-Nov-03 3:31 
GeneralRe: Comment Pin
avodavtyan23-Oct-07 3:33
avodavtyan23-Oct-07 3:33 
GeneralRe: Comment Pin
eyoung7029-Aug-04 18:52
eyoung7029-Aug-04 18:52 
GeneralThe other methods Pin
jam_snake10-Sep-03 15:32
jam_snake10-Sep-03 15:32 
GeneralRe: The other methods Pin
yantarian11-Nov-03 3:35
sussyantarian11-Nov-03 3:35 
GeneralRe: The other methods Pin
avodavtyan23-Oct-07 4:01
avodavtyan23-Oct-07 4:01 
GeneralSome more Pin
lee_connell10-Sep-03 14:40
lee_connell10-Sep-03 14:40 
Questionxml as database -max size? Pin
deodatus10-Sep-03 14:25
deodatus10-Sep-03 14:25 
AnswerRe: xml as database -max size? Pin
kay.one2-Dec-04 10:44
kay.one2-Dec-04 10:44 
AnswerRe: xml as database -max size? Pin
The Man from U.N.C.L.E.25-Jun-07 11:34
The Man from U.N.C.L.E.25-Jun-07 11:34 
GeneralNot enough Pin
dog_spawn4-Sep-03 13:08
dog_spawn4-Sep-03 13:08 
GeneralRe: Not enough Pin
lee_connell10-Sep-03 14:37
lee_connell10-Sep-03 14:37 

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.