Click here to Skip to main content
12,634,435 members (25,125 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked

Simple Yet Custom Configuration File

, 10 Apr 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
With very little coding, you have a customizable configuration and it is extendable


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" ?>
<section name="HSONG" type="com.hsong.Configuration.MyConfigHandler, tcustconfig"
restartOnExternalChanges="true" />
<!--<span class="code-comment"> CHANGE YOUR CONSTOM SECTION AS YOU WISH --></span>
<!--<span class="code-comment">END OF YOUR CONSTOM SECTION AS YOU WISH --></span>

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
            if (cfgNode != null) return;
            cfgNode = value;
            theConfig = new MyProjConfig();

    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=
        string va = cfg.GetValue("APPSETA"); // WHATEVER XPATH YOU MATCH YOUR CONFIG..
        string vb = cfg.GetValue("APPSETB/ANOTHERX");
        string vc = cfg.GetValue("APPSETB");


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.


  • 10th April, 2006: Initial post


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


About the Author

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

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 10 Apr 2006
Article Copyright 2006 by hsongrice
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid