Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NoteBook using XML and ADO.NET

0.00/5 (No votes)
22 Dec 2002 1  
An article on using XML and ADO.NET to create an appointment/address book.

Sample Image - xml_notebook.jpg

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 is 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 contain 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_Column and the second column is a link to the first table. The links in the first and second table will contain 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 occurrences 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 determines 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.

// create an object to hold the contents of the xml file
xmlDoc = new XmlDataDocument();

// creating an object to hold the dataset
ds = xmlDoc.DataSet;

// loading the schema to the dataset.
// in order to make holding only the data related to the schema
ds.ReadXmlSchema(xsdFilePath);

// loading all tables to the xmlDoc
// and only tables in the schema to the ds
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 a database, making it a middle tier between the database and the application. When the application is started, then all needed data is brought from the database to the XML file and the connection with the database is closed. When the application finishes, all changes 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 with knowledge of lot of programming languages and an expert in PHP language.

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 found here.

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 found here