Click here to Skip to main content
Click here to Skip to main content
Go to top

NoteBook using XML and ADO.NET

, 22 Dec 2002
Rate this:
Please Sign up or sign in to vote.
An article on using XML and ADO.NET to create an appointment/address book.
<!-- Download Links --> <!-- Article image -->

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


	// 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 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.

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

Share

About the Author

s972687(KFUPM)

United States United States
No Biography provided

Comments and Discussions

 
QuestionFullName field issue Pinmember@PADRINO@3-Aug-06 7:33 
GeneralSimple useful sample PinmemberAlexander Vishnevsky1-Jul-03 2:43 
GeneralRe: Simple useful sample Pinmemberagnihotrishweta7-Nov-03 6:47 
Generalhi, nice PinmemberMahmoud Nasr Ahmed27-May-03 11:38 
GeneralLoad a dataset created with a xsd tool from a database! Pinmemberthecappuccinokid31-Mar-03 3:18 
GeneralSome tips/questions Pinmemberleppie24-Dec-02 11:20 
Hi
 
1. Why arent you using the generated typeddataset?
2. DataSet has WriteXml/ReadXml methods. No need for all that extra code.
3. What are the "extra" 2 XSD schemas for?
 
WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support.

GeneralRe: Some tips/questions Pinmembers972687(KFUPM)24-Dec-02 23:58 
GeneralRe: Some tips/questions Pinmemberleppie25-Dec-02 7:08 
GeneralRe: Some tips/questions Pinmembers972687(KFUPM)26-Dec-02 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 23 Dec 2002
Article Copyright 2002 by s972687(KFUPM)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid