Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / XML
Article

XML Solution for the INI File System

Rate me:
Please Sign up or sign in to vote.
2.00/5 (9 votes)
28 Aug 20072 min read 32.4K   280   16   4
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.

C#
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)
C#
//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
C#
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

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralINI Helper Pin
Vasudevan Deepak Kumar9-Sep-07 2:50
Vasudevan Deepak Kumar9-Sep-07 2:50 
GeneralRe: INI Helper Pin
Jeremy Hiestand9-Sep-07 10:07
Jeremy Hiestand9-Sep-07 10:07 
GeneralRe: INI Helper Pin
Sk8tz11-Sep-07 23:01
professionalSk8tz11-Sep-07 23:01 
Agreed, also good ini, is much quicker to manipulate.

I use XML in majority of the cases(which its perfect for) is where there's a exchange of information needed.

Sk8tZ

GeneralFigures I'd find somthing like this Pin
Spacix One4-Sep-07 5:11
Spacix One4-Sep-07 5: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.