Introduction
This code is to demonstrate how to use xml file as a database. It uses ADO.NET
to access data inside the xml file. Also it uses xml schema to determine the
data that will be loaded to the dataset.
The code uses these technologies to develop a Notebook. This notebook consists
of two things. First thing is an Addresses keeper; the user can save
information about other people on it. The second part of the Notebook is the
Appointments part; the user can store all his important appointments or dates.
Background
This project is using XML (*.xml) as a database to hold the
data and XML Schema (*.xsd) to control the contents of the database.
XML Schema is also used to control the contents of the dataset, because there
are data that shouldn't be loaded from the XML file to the dataset then we can
restrict these data by assigning an XML Schema to the dataset then load
the contents of the xml file. Only the designated data in the XML Schema will
be loaded to the dataset object.
The data set will change the order of the data and it will make them in tables
instead of what they were. This will make accessing them easier. If there is an
XML field that may appear more than one time (multi value field) example :
<DatabaseNAME>
<TableName>
<field>1</field>
<field>2</field>
</TableName>
</DatabaseNAME>
Here in this part of the XML file when loaded to the dataset it
will create two tables, first table is named TableName and
it will contains one column that is linked to the second table, the second
table will be created because the <field> tag appears more than one
time under the same table so the dataset will create a table called field with
two columns first column is field_Colum and the second column is a link to the
first table. The links in the first and second table will contains the same
key.
The data set will create the tables according to the design in the XML Schema.
In the XML Schema it is possible to determine the number of occurrence for
a field.
To understand the code you should first see the schema files that are within the
project.
Using the code
The code is documented with the XML Documentation and so it is easy to
create documentation from the comments. Also I did comments on each step
of the code to show what it does. I am sure you will find reading the code easy.
This project consists of three types of files; the first type is the XML
file. It is only one file and it holds the database. This file
must exist in the project even if it is empty, its name is "AllTables.xml".
The second group is the XML Schema, they are three files. The first file is
"1AllTables.xml" it controls the contents of the XML file. The second file is
"AddressBookTables.xsd", it determine the tables that are related to the Address
Book. This file is loaded to the dataset before loading the XML file when we
need only information about addresses. The third file is "AppointmentTable.xsd"
and it is similar to the second file.
The third group of files is the C# files, there are 6 files. Five of these six
files are related to the forms and their functionalities and one file with the
name "NBDatabase.cs" is a class that handles all functions that are related to
the database.
I will show you part of the code that relates to loading the xml file and the
dataset. The order is important in this code.
xmlDoc = new XmlDataDocument();
ds = xmlDoc.DataSet;
ds.ReadXmlSchema(xsdFilePath);
xmlDoc.Load(xmlFilePath);
Conclusion
This project demonstrates how to use an XML file as database with ADO.NET. This
project is a Note Book that enables the user to store information and retrieve
them. XML is not good as a complete database, especially if the data sets are
huge. XML is useful, regarding to a database, making it a middle tier between
the database and the application. When the application is started then all
needed data are brought from the database to the xml file and the connection
with the database is closed. When the application finishes all changed to
the XML file are applied to the database. This project shows how to deal with
the XML file.
About the Project
I built this project for my doctor, Dr. Paul D. Manuel.
About the Author
A Computer Science student at King Fhad University of Petrolume &
Minerals in Saudi Arabia. Have background about lot of programming languages
and expert in PHP language.