Click here to Skip to main content
Licence CPOL
First Posted 10 Apr 2006
Views 20,056
Bookmarked 6 times

Simple Yet Custom Configuration File

By | 10 Apr 2006 | Article
With very little coding, you have a customizable configuration and it is extendable

Introduction

I always like the philosophy of simplicity. The configuration file serves the purpose of changing program behavior without touching code. The less code touched, the better.

In order to reach that simple goal, it is enough to just use an XmlNode to present configuration section. Any configuration extension, schema change will not impact that piece of configuration code. Certainly you can take the route of ConfigSection etc. yet, it defeats the simplicity and code changes are not welcomed easily for people in support roles.

For performance enhancement, in case you access the config value lots of times, you can put a little touch up on the stuff given below.

The Configuration

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="HSONG" type="com.hsong.Configuration.MyConfigHandler, tcustconfig"
allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<!-- CHANGE YOUR CONSTOM SECTION AS YOU WISH -->
<HSONG>
<APPSETA>JUNKA</APPSETA>
<APPSETB>
<ANOTHERX>JUNKB</ANOTHERX>
</APPSETB>
</HSONG>
<!--END OF YOUR CONSTOM SECTION AS YOU WISH -->
</configuration>

Here is a little explanation: What the above <section> is telling is that a class about the custom section of HSONG can be loaded. The class is com.hsong.Configuration.MyConfigHandler, and the assembly is tcustconfig.

Well, the next step is to define the very class that handles configurations (com.hsong.Configuration.MyConfigHandler).

IConfigurationHandler and Code Example

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;

namespace com.hsong.Configuration
{
public class MyProjConfig
{
    private static MyProjConfig theConfig;
    private static XmlNode cfgNode = null;
    internal static XmlNode ConfigXmlNode
    {
        set
        {
            if (cfgNode != null) return;
            cfgNode = value;
            theConfig = new MyProjConfig();
            ConfigurationManager.GetSection("HSONG");
        }
    }

    public static MyProjConfig GetConfig()
    {
        return theConfig;
    }

    public int GetCount(string xmlpath)
    {
        return cfgNode.SelectNodes(xmlpath).Count;
    }

    public string GetValue(string xmlpath, params int[] index)
    {
        System.Xml.XmlNodeList ndlist = cfgNode.SelectNodes(xmlpath);
        int nnodes = ndlist.Count;
        if (nnodes == 0) return null;
        int nindex = (index == null || index.Length == 0) ? 0 : index[0];
        System.Xml.XmlNode nx = ndlist.Item(nindex);
        return nx.InnerXml;
    }
}

public class MyConfigHandler : IConfigurationSectionHandler
{
    public object Create(object parent, object context, XmlNode node)
    {
        MyProjConfig.ConfigXmlNode = node;
        return MyProjConfig.GetConfig(); // cause it to load config from node.
    }
}

}
// example use, obtain config value by the xpath...
namespace tcustconfig
{
class Program
{ // example use
    static void Main(string[] args)
    {
        com.hsong.Configuration.MyProjConfig cfg=
                (com.hsong.Configuration.MyProjConfig)
        System.Configuration.ConfigurationManager.GetSection("HSONG");
        string va = cfg.GetValue("APPSETA"); // WHATEVER XPATH YOU MATCH YOUR CONFIG..
        string vb = cfg.GetValue("APPSETB/ANOTHERX");
        string vc = cfg.GetValue("APPSETB");
        //
    }
}
}

Conclusion

Keep it simple and easy for fellow developers to work with. When your team has lots of code to worry about, you want to have your team focused on business, not trick playing.

History

  • 10th April, 2006: Initial post

License

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

About the Author

hsongrice



United States United States

Member

Try my link in www.maxew.com. I have stolen some good articles, and will not hesitate to copy it handy there as my reference.

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
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Apr 2006
Article Copyright 2006 by hsongrice
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid