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

Using XML as Database with Dataset

Rate me:
Please Sign up or sign in to vote.
2.59/5 (19 votes)
20 Apr 20061 min read 145.4K   11.4K   55   2
How to make simple Database driven application with XML plain text. Portable, easy and light weight!

Sample Image - XMLData.jpg

Introduction

XMLData is small code to access XML file as a small database. If your application is small, doesn't need very secure access, and do you want user doesn't need to install and maintainance any database engine. XMLData provide provide simple access using the power of ADO.NET Dataset.

Using the code

This code using XML file named Category.xml as table. Every xml file represented by one table or domain entity. It's similar with xml mapping in many OR/mapper. But this xml file also contains data.

First time I provide Data Access Layer which access XML file. XMLCategory is helper class. This class provide CRUDs method to XML file.

C#
//Write any data (new or update data) to XML file
public static void save()

{

ds.WriteXml(Application.StartupPath + "\\XML\\Category.xml", XmlWriteMode.WriteSchema);

}

//Store new data in a dataview
public static void Insert(string categoryID, string CategoryName)

{

DataRow dr = dv.Table.NewRow();

dr[0] = categoryID;

dr[1] = CategoryName;

dv.Table.Rows.Add(dr);

save();

}

//Search any data in dataview with specific primary key, and update it's data
public static void Update(string categoryID, string CategoryName)

{

DataRow dr = Select(categoryID);

dr[1] = CategoryName;

save();

}

//Delete any data with specific key
public static void Delete(string categoryID)

{

dv.RowFilter = "categoryID='" + categoryID + "'";

dv.Sort = "categoryID";

dv.Delete(0);

dv.RowFilter = "";

save();

}

//Search any data in dataview with specific primary key
public static DataRow Select(string categoryID)

{

dv.RowFilter = "categoryID='" + categoryID + "'";

dv.Sort = "categoryID";

DataRow dr = null;

if (dv.Count > 0)

{

dr = dv[0].Row;

}

dv.RowFilter = "";

return dr;

}

//Load all data to dataset
public static DataView SelectAll()

{

ds.Clear();

ds.ReadXml(Application.StartupPath + "\\XML\\Category.xml", XmlReadMode.ReadSchema);

dv = ds.Tables[0].DefaultView;

return dv;

}


The next class is Categorylist which wrap XML class (facade class). We can provide any business logic in this class. This code is very simple, so no business logic. I just call appropriate method in xml helper class and wrap them. For the example, GetMethod is a method that return single object according any category id.

C#
public static Category GetCategory(string categoryID)

{

DataRow iDr = null;

iDr = XMLCategory.Select(categoryID);

Category cat = null;

if (iDr != null)

{

cat = new Category();

cat.CategoryID = iDr[0] != DBNull.Value ? iDr[0].ToString() : string.Empty ;

cat.CategoryName = iDr[1] != DBNull.Value ? iDr[1].ToString() : string.Empty;

}

return cat;

}


And the last is UI layer, we just call any method in wrapper class and bind any data (for example Dataview) to UI control.

Conclusion

Dataset provide simple access to xml in easy way. Using XML file as database only recommeded to simple and light application. It's will be useful if your only have small data. But if you have millions data, this method isn't recommeded because all of data will be load to memory. It's will be consuming very big memory. If you concern to security, you can encrypt the data use the classes in the <mshelp:link tabindex="0" keywords="N:System.Security.Cryptography.Xml">System.Security.Cryptography.Xml namespace. For small application, this code very cool to representing demo, prototype, and other small application without any database engine application prerequest. Happy XML! 

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
Indonesia Indonesia
Bernad Pakpahan stay in Jakarta Indonesia. Working as .NET Developer in PT. Netway Utama, one of ISV leader in Indonesia. Have personal interest with C#, ASP.NET,SOA,Design Pattern, and WinFX. You can contact Bernad at bern4d@gmail.com. You can see other bernad article in his community blog at http://blogs.netindonesia.net/bernardpakpahan

Comments and Discussions

 
Questionpls help Pin
Kadar Barna25-Nov-11 19:11
professionalKadar Barna25-Nov-11 19:11 
GeneralDatagrid index never changes Pin
Jeffrey Scott Flesher19-Feb-07 15:22
Jeffrey Scott Flesher19-Feb-07 15:22 
Great idea; but the Datagrid index always goes back to 0; same thing if I bind it to a ComboBox; any idea how to solve that?

Lessons learned from 911:

1. United We Stand.

2. United’s We Fall.

Gulf War Syndrome survivors never have a good day. http://www.vetshelpcenter.com/

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.