Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Yet Custom Configuration File

0.00/5 (No votes)
10 Apr 2006 1  
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 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