65.9K
CodeProject is changing. Read more.
Home

XML Solution for the INI File System

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (9 votes)

Aug 28, 2007

2 min read

viewsIcon

32642

downloadIcon

281

An attempt to use the common XML language to replace an ageing INI system

Sample Image - maximum width is 600 pixels

Introduction

After developing in Delphi for many years, I decided to learn a new technology. I decided to start with C# 2.0 and ASP.NET. After reading my first book on C# 2.0, I wanted to start small and develop a class library that phrases an XML file to save settings in future applications I develop. I wanted to use as much OOP as I could, starting with an interface and a class library.

Background

Here were the goals to accomplish in order to obtain the same "feel" as in the INI methods. I have a bit of refining to do but, all in all, this is a good starting point.

  1. Simple instance creation, single parameter (FileName)
  2. Simple method calling to Read and Write values
  3. Error trapping (return default values)

The class library makes use of:

  • XMLDocument
  • XmlElement
  • XmlNode

This class allows you to read and write strings. I know you're saying great, this only saves strings! There are 10 overloads of the write method to handle Int16/UInt16 to Int64/UInt64, bool, byte, float, decmial and of course string. Reading the values back is also possible.

Using the Code

When creating an instance of the class, you must pass the name of the XML file that will be used to read and write the values. The file will be created if it does not exist. If this fails, it will throw an exception of "Error phrasing file: " + this.xmlFileName.

XMLSettings myXMLsettings = new XMLSettings("myxml.xml");

Once the instance has been created, you can use the method calls to write values to the file. The WriteXML methods all take 3 parameters:

  1. string Parent: the Parent node, AKA [Section]
  2. string Key: the Key holds the location of the Value
  3. Value: the Value to be written (common value types supported)
//Write a string

myXMLsettings.WriteXMLString("ParentNode", 
    "MainTitle", "Demo of the XMLSettings Class" );

//Write an Integer

myXMLsettings.WriteXMLValue("ParentNode", "SizeX", 600 );
    
//Write bool

myXMLsettings.WriteXMLValue("ParentNode", "ShowTextBox", True );

Now that we have an XML file that is populated with settings, we can read back the values and use them as such. The ReadXML methods all take 3 parameters:

  1. string Parent: the Parent node, AKA [Section]
  2. string Key: the Key holds the location of the Value
  3. Default: the Value to be returned if an error in processing occurred
string strFormText;
FormText = myXMLsettings.ReadXMLString("ParentNode", "MainTitle", "");
    
int intSizeX;
intSizeX = myXMLsettings.ReadXMLInt32("ParentNode", "SizeX", 0);

bool bolShowTB;
bolShowTB = myXMLsettings.ReadXMLbool("ParentNode", "ShowTextBox", false);

If you specify a ParentNode that does not exist, it will create that Parent and place the Key and Value inside the new ParentNode.

Points of Interest

I was pleased to find how intuitive OOP can be; for many beginners I know it can get frustrating. Since this is my first "real" piece of tangible C# code that I used to learn with, I wanted to share it with others and hopefully they (you) can learn from it as well. I do have to note that this code undermines the hierarchy of an XML document in that it only allows for 2 parent nodes to be formed. My reason was to "mimic" the INI file structure.

History

  • 28 August, 2007 -- Original version posted