Click here to Skip to main content
13,297,547 members (49,815 online)
Click here to Skip to main content
Add your own
alternative version


80 bookmarked
Posted 11 Mar 2010

C# Application with XML File

, 9 May 2010
Rate this:
Please Sign up or sign in to vote.
Using an XML file as a database

Update (5/10/2010)

New code available for C# 2010 Express (cleaned up version also). Download the 2010 version, if 2008 code gives you problem in 2010.


This is my first article about my very first C# study application. While I was reading books and watching videos, I thought of creating something that I would also use, nothing fancy, simple to use. This application can be used for notes as well, not only for codes. The code itself, not the application and its functionality, is the most important part, and will be the main part of my following articles (if I am approved by you... the readers).


I started this code using Access DB. Then I purchased a laptop that came with 64bit Win7 OS. This is my first 64 bit machine and I didn't know how my code would react. Apparently there are some issues with Microsoft Jet engine vs 64 bit systems. Even though I found out some fixes and fix-arounds, I decided to move on to other things. I tried SQL Express... very powerful database, I love SQL. I used SQL servers for my previous applications (not in C#), and I love it. Although, I wanted this program to be very light and very "portable + Sharable" without any pain (or less pain). So, instead of using a "Home-made" text based DB, I decided to use XML. This way, if someone wants to transfer XML into another database, they would be able to do so easily. This is my first time dealing with XML, and I enjoyed it a lot.

Using the Code

There are two forms and two custom classes for this program:

  • Form1 (Main form)
  • Form Settings
  • IO class
  • XML class

The application lets you create a new XML file with three fields (ID, Code subject and main code) for its database, or locate an existing one. So, if multiple users are using different databases, they can share their XML files, also one can be shared between applications throughout the office or home. I didn't put any restrictions, such as log-ins and/or user levels to make changes. Maybe some of you want to add an option to give the user the flexibility of creating as many fields they want for their own purposes as well... all those things can be added into this code. Sky is the limit. :)

Since the entire code is available for download, I would like to show a few methods/codes here that I like because of their work.

Tooltip setup: I use "_toolTip" class in almost all my "form load" methods to show control tips for my users. Simple, but since this is a beginners' article, I thought maybe some of you might have just missed it. There is also "#region" "#endregion" usage here. It's a very nice tool to pack some of your code out of the way.

#region ToolTips
	 // Set some tooltips here
	_toolTip.SetToolTip(btnCopy, "Copy to ClipBoard");
	_toolTip.SetToolTip(btnNew, "Add new code with this subject");
	_toolTip.SetToolTip(btnExit, "Exit program");
	_toolTip.SetToolTip(lstSubjects, "Click to see the code");
	_toolTip.SetToolTip(chkOnTop, "Check to keep this window top of all windows");
	_toolTip.SetToolTip(btnSearch, "Search DB");
	_toolTip.SetToolTip(txtCodeMain, "Double Click to copy code into clipboard");

Resizing and positioning the form: Even there is "StartPosition" property for Windows forms, I still use following code to adjust my form size using current screen bounds.

// Resize the form using current screen size
this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
this.Height = Screen.PrimaryScreen.Bounds.Height - 300;

I use "Split" a lot in my codes. Sometimes it is much easer to pass information throughout classes and methods. Split also a "cheap" way to pass field information, I believe it does take less memory than arrays. Finding the right character is the thing, which I used "backspace" character here, since it can't be passed through a textbox.

XMLDatabase clsXML = new XMLDatabase(); // Create instance of XMLDatabase class
ArrayList CurrentSubjectList = new ArrayList(); // Create instance of ArrayList
                                                // This will loaded with the ArrayList
                                                // sent by clsXML object
string[] SplitText = new string[2]; 	// Each item in the array will contain a string
                                    	// that will be split by 'Backspace' character

CurrentSubjectList = clsXML.GetSubjectList
	(clsIO.XMLPath, SearchWord); // Get the ArrayList from clsXML
char Splitchr = (char)8; // 'char 8' is Backspace character.

foreach (string FullString in CurrentSubjectList) // Start looping the ArrayList
    SplitText = FullString.Split(Splitchr); 	// Split Item
    ListViewItem s_Items; 		// Set ListView item to add new information
    s_Items = lstSubjects.Items.Add
	(SplitText[0]); 	// Add Subject ID to the first (not visible) column
    s_Items.SubItems.Add(SplitText[1]); // Add Subjectname to second (visible) column

ListView control is one of my favorite controls (above). When used with "Details" mode, it can be a very nice tool to show items for my database fields. I don't like bounding data, so everything is dynamically coded to fill ListView control... even when I use SQL or Access database. No bounding... I hate bounding things to my application. The application must be totally free of ties between any data source, and connection should be done after program runs, when needed... (totally personal).

I probably won't repeat all this "simple" information for my next articles, but they're extremely useful things which some of you might be missing.

Here is another idea to change some of the control properties depending on a condition. It's a class that enables/disables controls during runtime.

private void EnableDisableButtons(bool EDButtons) 	// Change some of the 
						// control properties here
    // Menu items
    TSAddNew.Enabled = EDButtons;
    TSEdit.Enabled = EDButtons;
    TSDelete.Enabled = EDButtons;
    TSRefresh.Enabled = EDButtons;
    // Buttons
    btnSearch.Enabled = EDButtons;
    btnNew.Enabled = EDButtons;
    btnEdit.Enabled = EDButtons;
    btnDelete.Enabled = EDButtons;

    // Main codeview
    txtCodeMain.Enabled = EDButtons;
    txtSubject.Enabled = EDButtons;


The following code shows "overload" class. I had to keep the original constructor and add another one, because I need to set some of the control values during instantiation.

public frmSettings()


// Settings form constructor overload
public frmSettings(string OnTopStr, string WordWrapStr, string AutoSaveStr)

    // Set some control values
    if (OnTopStr == "1")
    { chkOnTop.Checked = true; }

    if (WordWrapStr == "1")
    { chkWordWrap.Checked = true; }

    if (AutoSaveStr == "1")
    { chkAutoSave.Checked = true; }

    IOFile clsIO = new IOFile(); // Create instance of IOFile class

    if (clsIO.ErrHap == null)
        Scripting.FileSystemObject FsO = 
	new Scripting.FileSystemObject(); // Create instance of FileSystemObject class
        lblXML.Text = "XML File Name : " + 
	FsO.GetFileName(clsIO.XMLPath); // XML file name into label box
        XMLDatabaseLocation = clsIO.XMLPath; // Set XML file path

The rest of the article will show XML file manipulations. XML portion is probably the only place my code gets complicated(!). I hope you will find it useful.

Create an XML file with 3 child nodes. So, there is a main tag, a parent tag and three children tags.

XmlTextWriter XMLWrite = new XmlTextWriter(XMLPath, System.Text.Encoding.UTF8);

XMLWrite.WriteStartElement("Codes"); // Main tag
XMLWrite.WriteStartElement("NewCode"); // Parent tag
XMLWrite.WriteElementString("ID", "1"); // Child 1
XMLWrite.WriteElementString("DBSubject", "Welcome"); // Child II
	"Thank you for using CodeDatabase by Leo Koach (2010)"); // Child III


Appending records (nodes) at the end of the XML file:

XmlDocument xmlDoc = new XmlDocument();
XmlElement subRoot = xmlDoc.CreateElement("NewCode");

XmlElement appendedElementID = xmlDoc.CreateElement("ID");
XmlText xmlTextID = xmlDoc.CreateTextNode(GetLatestID(XMLPath));

XmlElement appendedElementSubject = xmlDoc.CreateElement("DBSubject");
XmlText xmlTextSubject = xmlDoc.CreateTextNode(CodeSubject);

XmlElement appendedElementCode = xmlDoc.CreateElement("DBCode");
XmlText xmlTextCode = xmlDoc.CreateTextNode(CodeMain);


There might be another way to edit a node information, although this looks pretty good and works just fine for me. All I am doing here is to loop XML nodes and search for ID field for the ID I'm passing. If a match is found, change the selected field information with the new one.

XmlDocument XMLEdit = new XmlDocument();

XmlElement XMLEditNode = XMLEdit.DocumentElement;

foreach (XmlNode node in XMLEditNode) // Loop through XML file
    if (node["ID"].InnerText == SubjectID) // Check for the ID field information
        node["DBCode"].InnerText = CodeMain;

Deleting a portion of the XML file is done as follows. Just like the above code, I am looking for the ID field. If I find it, I only delete the parent node that ID field and other children fields located. I probably could merge those two loops into one, but for learning purposes, this is ok. You're always welcome to do your own merging. That could save you writing extra lines of code.

XmlDocument XMLDelete = new XmlDocument();

XmlElement XMLDeleteNode = XMLDelete.DocumentElement;

foreach (XmlNode node in XMLDeleteNode)
    if (node["ID"].InnerText == SubjectID) // Find the match for the ID field
        node.ParentNode.RemoveChild(node); // Delete the parent for the children (fields)

Get a field information (node) from XML file, and throw an exception. I included a bit more code here to show how I used the Try/Catch and Exception thrower. When an exception is thrown like this, it comes back to the "sender" (I like to call it that way, it's actually sending the exception back to the method which class was initiated). Same as other three codes, I loop through the XML until I find my ID.

XmlDocument XMLRead = new XmlDocument(); // Create instance of XmlDocument class

    XMLRead.Load(XMLPath); // Set XMLRead object's path value

    XmlNodeList XMLItems = XMLRead.SelectNodes
	("Codes/NewCode"); // Create instance of XmlNodeList class
    // and set its node value

    foreach (XmlNode node in XMLItems) // Loop Node for the child node items
        if (node["ID"].InnerText == SubjectID) // Find the ID number
            StrInf = node["DBCode"].InnerText; // Set return value to 
					// Main Code value from the XML file
            break; // Exit loop
    return StrInf;
catch (Exception exc)
    throw exc;

By the way, the ID field always grows to higher numbers even if anything is deleted from the XML file. I could (and it is easy to do) find the missing number and assign that for the next ID (for example, I have data from ID number 1 through 40... and I deleted number 23. So there is a gap now. Next time when I enter a new data, I could just find number 23 and assign my ID to it). I like the way it is though. To get the latest number, I have another loop that reads the ID field only and adds "1".

public string GetLatestID(string xmlFilePath)
    int LastIDEntry = 0;
    XmlDocument XMLGetLastID = new XmlDocument();

    XmlNodeList XMLItems = XMLGetLastID.SelectNodes("Codes/NewCode");

    foreach (XmlNode node in XMLItems)
        LastIDEntry = Convert.ToInt32(node["ID"].InnerText) + 1;
    return Convert.ToString(LastIDEntry);

I know there are probably better ways than looping through XML files, which could slow things down when the file gets larger, and I need your help to find them (if any).

Points of Interest

This code is for C# beginners (like myself). I love coding and enjoy any challenges that come with it. I research a lot to learn (even at my age) a lot. These days, internet is my only source (99.90% of the time) to do research for C#. Hopefully some of you will copy/paste and use some of my code here. I like to modify the code I find to fit my code, which is probably the best way to learn (next to watching C# videos). I hope to see you at my next article "Study II". :)


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


About the Author

Leo Koach
Engineer Koach Software
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
AAB4020-Feb-17 5:07
memberAAB4020-Feb-17 5:07 
GeneralMy vote of 3 Pin
Nalin Tharanga Jayasinghe(THARA)12-Mar-14 7:53
memberNalin Tharanga Jayasinghe(THARA)12-Mar-14 7:53 
GeneralMy vote of 4 Pin
Anand10084-Feb-13 20:50
memberAnand10084-Feb-13 20:50 
GeneralNice Pin
Adriaan Davel17-May-10 20:22
memberAdriaan Davel17-May-10 20:22 
GeneralSupport Pin
Leo Koach18-Apr-10 9:37
memberLeo Koach18-Apr-10 9:37 
GeneralNeat stuff Pin
TheRaven16-Mar-10 14:33
memberTheRaven16-Mar-10 14:33 
GeneralCode Updates Pin
LeoKoach13-Mar-10 9:15
memberLeoKoach13-Mar-10 9:15 
GeneralNice! Pin
Member 4558663-Apr-10 0:47
memberMember 4558663-Apr-10 0:47 
GeneralSome advice Pin
Steve Hansen11-Mar-10 23:12
memberSteve Hansen11-Mar-10 23:12 
GeneralRe: Some advice Pin
LeoKoach12-Mar-10 4:37
memberLeoKoach12-Mar-10 4:37 
GeneralRe: Some advice Pin
LeoKoach12-Mar-10 22:22
memberLeoKoach12-Mar-10 22:22 
GeneralRe: Some advice Pin
Steve Hansen13-Mar-10 2:59
memberSteve Hansen13-Mar-10 2:59 
GeneralRe: Some advice Pin
LeoKoach13-Mar-10 3:59
memberLeoKoach13-Mar-10 3:59 
GeneralRe: Some advice Pin
Slimpis10-May-10 0:59
memberSlimpis10-May-10 0:59 
GeneralYour code would do everything it says but I wont use XML as a DB Pin
gaurav_verma_mca11-Mar-10 18:19
membergaurav_verma_mca11-Mar-10 18:19 
GeneralRe: Your code would do everything it says but I wont use XML as a DB Pin
LeoKoach11-Mar-10 18:42
memberLeoKoach11-Mar-10 18:42 
GeneralRe: Your code would do everything it says but I wont use XML as a DB Pin
gaurav_verma_mca11-Mar-10 19:16
membergaurav_verma_mca11-Mar-10 19:16 
GeneralRe: Your code would do everything it says but I wont use XML as a DB Pin
LeoKoach11-Mar-10 20:11
memberLeoKoach11-Mar-10 20:11 

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.171207.1 | Last Updated 10 May 2010
Article Copyright 2010 by Leo Koach
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid