0% found this document useful (0 votes)
89 views

Using XML As Database With Dataset - CodeProject

This document describes how to use XML files as a simple database for small applications. It presents a Data Access Layer class called XMLCategory that provides CRUD methods to read from and write to an XML file. A wrapper class called CategoryList contains business logic methods that call the XMLCategory methods. The code provides a way to access XML data using ADO.NET Dataset in a portable, easy to use manner without needing a full database for small or simple applications.

Uploaded by

ngoclinhtu9816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Using XML As Database With Dataset - CodeProject

This document describes how to use XML files as a simple database for small applications. It presents a Data Access Layer class called XMLCategory that provides CRUD methods to read from and write to an XML file. A wrapper class called CategoryList contains business logic methods that call the XMLCategory methods. The code provides a way to access XML data using ADO.NET Dataset in a portable, easy to use manner without needing a full database for small or simple applications.

Uploaded by

ngoclinhtu9816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UsingXMLas Database with Dataset

bern4d,20 Apr 2006

Rate:

2.5919votes

How to make simple Database driven application withXMLplain text. Portable, easy and light weight!

Is your email address OK?You are signed up for our newsletters but your email address is either
unconfirmed, or has not been reconfirmed in a long time. Pleaseclickhereto have a confirmation
email sentso we can confirm your email address and start sending you newsletters again.
Alternatively, you canupdate your subscriptions.

Download source files - 78 Kb


Download demo project 78 Kb

Introduction
XMLData is small code to accessXMLfile 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 andmaintainanceany database engine.XMLData provide
provide simple access using the power of ADO.NET Dataset.

Using the code


This code usingXMLfile named Category.xmlas table. Everyxmlfile represented by one table or domain entity. It's
similar withxmlmapping in many OR/mapper. But thisxmlfile also contains data.
First time I provide Data Access Layer which accessXMLfile.XMLCategory is helper class. This class provide CRUDs
method toXMLfile.
HideShrink

Copy Code

//Writeanydata(neworupdatedata)toXMLfile
publicstaticvoidsave()
{
ds.WriteXml(Application.StartupPath+"\\XML\\Category.xml",XmlWriteMode.WriteSchema);
}
//Storenewdatainadataview
publicstaticvoidInsert(stringcategoryID,stringCategoryName)
{
DataRowdr=dv.Table.NewRow();
dr[0]=categoryID;
dr[1]=CategoryName;
dv.Table.Rows.Add(dr);
save();
}
//Searchanydataindataviewwithspecificprimarykey,andupdateit'sdata
publicstaticvoidUpdate(stringcategoryID,stringCategoryName)
{
DataRowdr=Select(categoryID);
dr[1]=CategoryName;
save();
}
//Deleteanydatawithspecifickey
publicstaticvoidDelete(stringcategoryID)
{
dv.RowFilter="categoryID='"+categoryID+"'";
dv.Sort="categoryID";
dv.Delete(0);
dv.RowFilter="";
save();
}

//Searchanydataindataviewwithspecificprimarykey
publicstaticDataRowSelect(stringcategoryID)
{
dv.RowFilter="categoryID='"+categoryID+"'";
dv.Sort="categoryID";
DataRowdr=null;
if(dv.Count>0)
{
dr=dv[0].Row;
}
dv.RowFilter="";
returndr;
}
//Loadalldatatodataset
publicstaticDataViewSelectAll()
{
ds.Clear();
ds.ReadXml(Application.StartupPath+"\\XML\\Category.xml",XmlReadMode.ReadSchema);
dv=ds.Tables[0].DefaultView;
returndv;
}

The next class is Categorylist which wrapXMLclass facade class. We can provide any business logic in this class.
This code is very simple, so no business logic. I just callappropriate method inxmlhelper class and wrap them. For
the example, GetMethod is a method that return singleobject according any category id.
HideCopy Code

publicstaticCategoryGetCategory(stringcategoryID)
{
DataRowiDr=null;
iDr=XMLCategory.Select(categoryID);
Categorycat=null;
if(iDr!=null)
{
cat=newCategory();
cat.CategoryID=iDr[0]!=DBNull.Value?iDr[0].ToString():string.Empty;
cat.CategoryName=iDr[1]!=DBNull.Value?iDr[1].ToString():string.Empty;
}

returncat;
}

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

Conclusion
Dataset provide simple access toxmlin easy way. UsingXMLfile as database only recommeded to simple and light
application. It's will beuseful if your only have small data. But if you have millions data, this method isn't
recommeded becauseall ofdata 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 theSystem.Security.Cryptography.Xmlnamespace. For small
application, this code very cool to representing demo, prototype, and other small application without any database
engine application prerequest. HappyXML!

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 foundhere

You might also like