Click here to Skip to main content
13,556,507 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 23 May 2006
Licenced CPOL

Lightweight DB Framework

, 23 May 2006
Rate this:
Please Sign up or sign in to vote.
Manage small database tables with lightweight and simple C# code. Best suited for PocketPC and Windows Mobile devices


When I decided to write a small shopping list program ( for my own personal use, I planned to use Microsoft's Compact Framework for storing the data using XML serialization APIs. Unfortunately, I discovered that there was no such API available for the Compact Framework at the time. So since the amount of data I was planning to store was small, I decided to develop my own very simple database framework, instead of using some professional DB package that would be expensive and memory consuming.

Storage Files

Databases are stored as plain XML files. As mentioned, Compact Framework does not provide support for XML Serialization/Deserialization, so I had to implement a simple version myself, using XML readers and writers. Each database table is stored in a separate XML file. When a database table is loaded, the file is parsed and read entirely into memory. This makes it suitable for relatively small databases. When the database is saved, the entire memory content is written back into an XML file.

File Structure

In order to be able to access a unique record, each record in a database is identified by a unique sequential key. The NextKey sequential key is stored at the top of the database file, so that each new record that is added will use it and increment it for the usage of the next record added. All other fields are stored subsequently as regular XML elements. In the following example, there are two books in the database.

   <name>The Bible</name>
   <name>The Da Vinci Code</name>
   <author>Dan Brown</author>


Define Your Database Classes

Each database is comprised from two classes.

  1. The class that represents each data record. It extends the ItemBase class. The main role of this class is to define record members and handle serialization / deserialization of data from and into the members.

  2. The class that represents the container of the database records. We use the ListItemTypeAttribute to define the class that is contained in this container. It extends the ItemListBase class. In most cases, the class will not have any implementation. Here is a sample code that defines the Books database:

public class Book : ItemBase
        public string name;
        public string author;
        public string isbn;

        public Book()
        public Book(string name)
   = name;

        protected override void SerializeMembers(System.Xml.XmlTextWriter xmlWriter)
            xmlWriter.WriteElementString("name", name);
            xmlWriter.WriteElementString("author", author);
            xmlWriter.WriteElementString("isbn", isbn);

        public override void DeserializeMembers(System.Xml.XmlNodeReader xmlReader)

            if (xmlReader.IsStartElement("name"))
                 name = xmlReader.ReadElementString();
            if (xmlReader.IsStartElement("author"))
                author = xmlReader.ReadElementString();
            if (xmlReader.IsStartElement("isbn"))
                isbn = xmlReader.ReadElementString();

public class Books : ItemListBase
       public Books()

Creating Database and Adding items

Now that we have the Books database defined, we are ready to use it. First, we create an empty database.

Books books = new Books();

Now we define one Book record and add it to the Books database.

Book book1 = new Book("The Bible"); = "God";
book1.isbn = "000-000-001";

Key justAddedKey = books.Add(book1);

Save it into an XML file books.xml.


Loading Database and Retrieving Items

When we load a database from the XML file, the file is parsed according to the type of the database container that is passed. All database items are deserialized and stored in the database. The items are also hashed so that they can be accessed quickly using the database's sequential key. In this example, we load the Books database and get an item using a key.

books = (Books)Books.Load(typeof(Books), "books.xml");
Book searchedBook = (Book)books.GetItem(justAddedKey);


If you are considering writing a .NET app that uses a small personal database, especially if you plan to run it on a memory constrained device such as Pocket PC, you should consider this framework. It's pretty bare-boned, but it has the basic functionality to store and retrieve records from XML formatted files.


  • 24th May, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood Job Pin
Polymorpher1-Jun-06 13:00
memberPolymorpher1-Jun-06 13:00 
GeneralDataset can be saved to xml Pin
thapa da man29-May-06 16:55
memberthapa da man29-May-06 16:55 
GeneralXML serialization in CF Pin
Ravi Bhavnani24-May-06 1:31
memberRavi Bhavnani24-May-06 1:31 
GeneralRe: XML serialization in CF Pin
arie_schwartzman24-May-06 4:17
memberarie_schwartzman24-May-06 4:17 
GeneralRe: XML serialization in CF Pin
Ravi Bhavnani24-May-06 4:19
memberRavi Bhavnani24-May-06 4:19 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 24 May 2006
Article Copyright 2006 by arie_schwartzman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid