Click here to Skip to main content
Licence 
First Posted 20 Apr 2006
Views 70,791
Bookmarked 48 times

Using XML as Database with Dataset

By | 20 Apr 2006 | Article
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.

//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.

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 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

About the Author

bern4d



Indonesia Indonesia

Member

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

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
Questionpls help Pinmemberbarnas19:11 25 Nov '11  
GeneralDatagrid index never changes PinmemberJeffrey Scott Flesher15:22 19 Feb '07  

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
Web03 | 2.5.120517.1 | Last Updated 20 Apr 2006
Article Copyright 2006 by bern4d
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid