Click here to Skip to main content
Licence 
First Posted 28 Aug 2007
Views 17,250
Downloads 138
Bookmarked 16 times

XML Solution for the INI File System

By | 28 Aug 2007 | Article
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

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

About the Author

Jeremy Hiestand

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralINI Helper PinmemberVasudevan Deepak Kumar2:50 9 Sep '07  
GeneralRe: INI Helper PinmemberJeremy Hiestand10:07 9 Sep '07  
GeneralRe: INI Helper PinmemberSk8tz23:01 11 Sep '07  
GeneralFigures I'd find somthing like this PinmemberSpacixOne5:11 4 Sep '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 28 Aug 2007
Article Copyright 2007 by Jeremy Hiestand
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid