65.9K
CodeProject is changing. Read more.
Home

Using XML as Database with Dataset

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.59/5 (19 votes)

Apr 20, 2006

1 min read

viewsIcon

145905

downloadIcon

11462

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!