Xml file as a database part 2

Been a but busy lately but I didn’t give up on the idea of an xml database yet. Actually, I started with some code even before the first post – that is in principle working but it still needs a lot of work. Unfortunately I’ve been side-tracked on other things in the last couple of days so the whole idea is just sitting idle for the moment.

First let me cover some (old) basics. The following examples are nothing new and functionality available for quite a number of years using plain old DataSets.

The first concept is how to create define or create a DataSet in code or run-time. Look at the following example:

DataSet myDb = new DataSet(“XmlDb”);
DataTable personTable = new DataTable(“Person”);

DataColumn personId = new DataColumn(“Id”, typeof(int)) { AutoIncrement = true, AutoIncrementSeed = 1, Unique = true, AllowDBNull = false };
DataColumn surnameId = new DataColumn(“Id”, typeof(int)) { AutoIncrement = true, AutoIncrementSeed = 1, Unique = true, AllowDBNull = false };
DataColumn personSurnameId = new DataColumn(“SurnameId”, typeof(int)) { AllowDBNull = false };
DataColumn fatherId = new DataColumn(“FatherID”, typeof(int));
DataColumn motherId = new DataColumn(“MotherID”, typeof(int));

personTable.Columns.AddRange(new DataColumn[]
{

personId,
new DataColumn(“Name”, typeof(string)) { AllowDBNull = false},
personSurnameId,
new DataColumn(“YearOfBirth”, typeof(int)),
new DataColumn(“MonthOfBirth”, typeof(int)),
new DataColumn(“DayOfBirth”, typeof(int)),
new DataColumn(“Description”, typeof(string)),
fatherId,
motherId

});
personTable.PrimaryKey = new DataColumn[] { personId };
myDb.Tables.Add(personTable);

DataTable surnameTable = new DataTable(“Surname”);
surnameTable.Columns.AddRange(new DataColumn[]
{

surnameId,
new DataColumn(“Surname”, typeof(string)) { Unique=true, AllowDBNull=false }

});
surnameTable.PrimaryKey = new DataColumn[] { surnameId };
myDb.Tables.Add(surnameTable);
DataRelation personSurname = new DataRelation(“PersonSurname”, surnameId, personSurnameId);
myDb.Relations.Add(personSurname);
DataRelation personFather = new DataRelation(“PersonFather”, personId, fatherId);
myDb.Relations.Add(personFather);
DataRelation personMother = new DataRelation(“PersonMother”, personId, motherId);
myDb.Relations.Add(personMother);

The piece of code above shows a couple of things (which probably includes bad programming habits as well… but it is only an example 😉 ). First it creates a DataSet with two tables. The two tables have a relationship between them that will enforce some referential integrity. Both tables have an id field that is an autonumber/identity column. The ‘Person’ table also adds two internal references (parent-child relationship). No rocket science here (yet).

The next block demonstrates how to add raw data to the DataSet.

DataRow newSurname = surnameTable.NewRow();
newSurname[“Surname”] = “Van Der Merwe”;
surnameTable.Rows.Add(newSurname);

newSurname = surnameTable.NewRow();
newSurname[“Surname”] = “Botha”;
surnameTable.Rows.Add(newSurname);

DataRow newRow = personTable.NewRow();
newRow[“Name”] = “Piet”;
newRow[“SurnameId”] = 1;
newRow[“Description”] = “Testing 123”;
personTable.Rows.Add(newRow);

newRow = personTable.NewRow();
newRow[“Name”] = “Sannie”;
newRow[“SurnameId”] = 2;
newRow[“YearOfBirth”] = 1950;
personTable.Rows.Add(newRow);

newRow = personTable.NewRow();
newRow[“Name”] = “Koos”;
newRow[“SurnameId”] = 1;
newRow[“YearOfBirth”] = 1980;
newRow[“FatherID”] = 1;
newRow[“MotherID”] = 2;
personTable.Rows.Add(newRow);

Again, no rocket science here. Two surnames are added and then 3 people. The third person reference the first two (parents).

Then if you want  to ‘persist’ the DataSet:

if (saveFileDialogXml.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{

if (System.IO.File.Exists(saveFileDialogXml.FileName))

System.IO.File.Delete(saveFileDialogXml.FileName);

myDb.WriteXml(saveFileDialogXml.FileName, XmlWriteMode.WriteSchema);

Then lastly you might want to reload it back into another DataSet to add some more data etc.

DataSet myDb2 = new DataSet();
myDb2.ReadXml(saveFileDialogXml.FileName);

personTable = myDb2.Tables[“Person”];
newRow = personTable.NewRow();
newRow[“Name”] = “Gert”;
newRow[“SurnameId”] = 1;
personTable.Rows.Add(newRow);

So there you have the basics how a simple .Net DataSet can be used to create, load, persist, reload and change data to and from an Xml file. Personally I’d like to add another step to compress (zip) the data while on disk – to make it easier to transport if needed. This will not impact functionality or performance of the running application.

One concern is of course what happens when the ‘database’ grows big because if you haven’t noticed, the entire database must be loaded/saved when you interact with the Xml file. This concept will not allow you to use extremely large databases – but then it is not intended for mission critical enterprise database systems! On the positive side – the database is ‘in memory’ so it should be very fast. Splitting the file and loading only sections will make it very tricky and possibly mean you have to wrote a lot of custom code.

It is also possible to use a Typed DataSet in exactly the same manner. The advantage then is you don’t have to define the ‘database structure’ in code. The rest works basically the same.

  1. Xml file as a database – part 3 | Rudolf's mind - pingback on 2011/06/06 at 10:35

Leave a Reply

Trackbacks and Pingbacks:

%d bloggers like this: