Click here to Skip to main content
12,451,227 members (52,421 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked

Creating a custom intelligent configuration file

, 2 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on providing a tool to help web developers to create more flexible web applications.

ConfigurationEx - xml node

ConfigurationEx - Intelligence


In our web applications, we always maintain some fixed information such as page titles, page URLs, web site banner text, common settings etc. Storing such information in the web.config file is a good idea. However, retrieving these information from the web.config file is not easy. Also, there is no intelligence at all, so developers should be very carful to make sure the value they typed is actually the same as in the web.config file. In this article, I'll demonstrate how to create a configuration tool which provides a new way to create a custom, intelligent config file.


This article is for developers who want to:

  1. Store a fixed information in the config file instead of hard coding in the source code.
  2. Provide intelligence when typing custom setting values in the source code.
  3. Make custom setting values in the config file with strong typing in the source code.

Using the code

Step 1: Create a code generator.

The first thing to do is to create a code generator class.

/// <summary>
/// Custom code generator.
/// It will retrieve all the namespaces,classes and property from 
/// the configuration file(xml file)
/// </summary>
public class ConfigCodeGenerator

There are four key methods:

  • BuildNameSpace
  • BuildAllClasses
  • AddPropertyToClass
  • GenerateUnitCode

Implement the AddPropertyToClass method to add the property specified in the "Property" attribute of the "Class" node in the config file:

/// <summary>
/// Add properties to existed class
/// </summary>
/// <param name="objEntity">Existed class to add properties(reference)</param>
/// <param name="xmlndLs">Xml nodes which hold all properties </param>
/// <returns>True - success False - failed</returns>
private Boolean AddPropertyToClass(ref CodeTypeDeclaration objEntity, 
                                   XmlNodeList xmlndLs)
        if ( (objEntity != null) && (xmlndLs != null) && (xmlndLs.Count > 0))

            foreach (XmlNode xmlnd in xmlndLs)
                if(xmlnd.Name == "Property")
                    // Get field type
                    CodeTypeReference objFieldType = new CodeTypeReference(

                    // Get property name then lower it and add "_"
                    // in front of the string to make the field name 
                    String strPropertyName = xmlnd.Attributes["name"].Value;
                    String strFieldName = "_" + strPropertyName.ToLower();

                    // Get default value of the field
                    Object objValue = GetFieldDefaultValue(xmlnd.Attributes["value"].Value, 

                    // Get comments
                    String strComment = String.Empty;
                    if (xmlnd.Attributes["comment"] != null)
                        strComment = xmlnd.Attributes["comment"].Value;

                    // Generate field
                    CodeMemberField objField = new CodeMemberField();
                    objField.Attributes = MemberAttributes.Private | MemberAttributes.Static;
                    objField.Name = strFieldName;
                    objField.Type = objFieldType;
                    objField.InitExpression = new CodePrimitiveExpression(objValue);    // ???ֵ

                    // Generate property
                    CodeMemberProperty objProperty = new CodeMemberProperty();
                    objProperty.Attributes = MemberAttributes.Public | MemberAttributes.Static;
                    objProperty.Name = strPropertyName;
                    objProperty.Type = objFieldType;
                    objProperty.GetStatements.Add(new CodeSnippetStatement("return " + 
                                                  strFieldName + ";"));
                    objProperty.Comments.Add(new CodeCommentStatement("<summary>", true));
                    objProperty.Comments.Add(new CodeCommentStatement(strComment, true));
                    objProperty.Comments.Add(new CodeCommentStatement("</summary>", true));

                    // Add field and property to class

            return true;

        return false;
    catch(Exception ex)
        throw new Exception(ex.Message + "\r\nFailed to add properties to class");

Implement BuildAllClasses to create the classes which are specified in the "Class" nodes in the config file:

/// <summary>
/// Recursive search all the nodes in the xml file 
/// to find the useful elements 
/// </summary>
/// <param name="xmlndRoot">Root of the xml file</param>
/// <param name="objTopEntity">The topmost class in the unit code</param>
private void BuildAllClasses(XmlNode xmlndRoot, ref CodeTypeDeclaration objTopEntity)
    foreach (XmlNode xmlnd in xmlndRoot.ChildNodes)
        if (xmlnd.Name == "Class")
            CodeTypeDeclaration objSubEntity = null;
            // add child class
            if(xmlnd.Attributes["comment"] != null)
                objSubEntity = BuildEmptyClass(xmlnd.Attributes["name"].Value, 
                objSubEntity = BuildEmptyClass(xmlnd.Attributes["name"].Value);

            BuildAllClasses(xmlnd, ref objSubEntity);
        else if (xmlnd.Name == "Property")
            // add property
            AddPropertyToClass(ref objTopEntity, xmlndRoot.ChildNodes);

Implement BuildNameSpace to create the only namespace specified in root node in the config file:

/// <summary>
/// Build namespace
/// </summary>
/// <param name="xmlConfigFile">configuration file</param>
/// <returns>the generated namespace</returns>
private CodeNamespace BuildNameSpace(XmlDocument xmlConfigFile)
        // The root is really the 2nd child node in the xml file cause the 1st one
        // is ""
        XmlNode xmlndRoot = xmlConfigFile.ChildNodes[1];

        // Build namespace use the root name
        CodeNamespace objNameSpace = new CodeNamespace(xmlndRoot.Name);

        // The topmost class
        CodeTypeDeclaration objTopEntity = null;
        if(xmlndRoot.Attributes["comment"] != null)
            objTopEntity = BuildEmptyClass(xmlndRoot.Name, 
            objNameSpace.Comments.Add(new CodeCommentStatement("<summary>", true));
                         CodeCommentStatement(xmlndRoot.Attributes["comment"].Value, true));
            objNameSpace.Comments.Add(new CodeCommentStatement("</summary>", true));
            objTopEntity = BuildEmptyClass(xmlndRoot.Name);

        // Build all classes in the namespace
        BuildAllClasses(xmlndRoot, ref objTopEntity);

        // Add the topmost class to the namespace
        // Notice: objTopEntity already include all the subclasses and properties

        return objNameSpace;
    catch(Exception ex)
        throw new Exception(ex.Message + " Failed when execute BuildNameSpace");

Finally, call BuildNameSpace in GenerateUnitCode:

/// <summary>
/// Generate unit code
/// </summary>
/// <param name="xmlConfigFile">xml file to be parsed</param>
/// <returns></returns>
public CodeCompileUnit GenerateUnitCode(XmlDocument xmlConfigFile)
    if (xmlConfigFile == null)
        throw new ArgumentNullException("configex");

    // Build the only namespace
    CodeNamespace objNameSpace = BuildNameSpace(xmlConfigFile);
    // Generate the unit code
    CodeCompileUnit objCompileUnit = new CodeCompileUnit();

    return objCompileUnit;

Step 2: Inherit from BuildProvider and override the GenerateCode method to add a custom code unit:

/// <summary>
/// Custom config provider
/// </summary>
[PermissionSet(SecurityAction.Demand, Unrestricted = true)]
public class ConfigProvider : BuildProvider

    public ConfigProvider()

    /// <summary>
    /// Override this method to generate own unit code
    /// </summary>
    /// <param name="assemblyBuilder">Assembly builder provided by the base class</param>
    public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        XmlDocument configFile = new XmlDocument();

            using (Stream file = VirtualPathProvider.OpenFile(this.VirtualPath))

        // Create a code generator to generate the whole code
        ConfigCodeGenerator objCodeGenerator = new ConfigCodeGenerator();

        // Generate the unit code and add it to the assembly builder

How to use this tool

Here are the steps:

  1. Copy configurationEx.dll to the ASP.NET project's Bin folder.
  2. Copy configex.xsd and configex.xsx to the ASP.NET project's App_Code folder.
  3. Add this code in the compilation node of the web.config file:
        <add extension=".configex" type="ConfigurationEx.ConfigProvider"/>
  4. Create an XML file (for example: MyConfig.configex), and add it to the App_Code folder.
  5. Open MyConfig.configex, choose configex.xsd as its schema.
  6. Write your own config values. Here is an example used by the demo project:
    <?xml version="1.0" encoding="utf-8" ?>
    <WebsiteData xmlns="" 
                 comment="Custom website settings">
      <Class name="PageUrlGroup" comment="All page urls in this website">
        <Property name="MainPage" value="~/Default.aspx" 
                  type="string" comment="Main page url"/>
        <Property name="SecondPage" value="~/SecondPage.aspx" 
                  type="string" comment="Second page url"/>
      <Class name="PageTitles" comment="Titles displayed in the brower">
        <Property name="MainPage" value="ConfigurationEx Test Page" 
                  type="string" comment="Main page title"/>
        <Property name="SecondPage" value="Second Page" 
                  type="string" comment="Second page title"/>
      <Class name="ButtonText" comment="Collection of button text">
        <Property name="ToSecondPage" value="Go to second page" type="string"/>
        <Property name="ToMainPage" value="Back to main page" type="string"/>
      <Class name="PageInfomation" comment="Collection of page infomation">
        <Property name="Title" value="Thanks to use ConfigurationEx!" 
                  type="string" comment="Main page title"/>
        <Property name="Banner" value="This is a banner infomation from custom config file" 
                  type="string" comment="Banner infomation"/>
  7. Use your configuration in the source code. For example:
    protected void Page_Load(object sender, EventArgs e)
        if ( !IsPostBack )
            // Initialize page data
            Title = WebsiteData.WebsiteData.PageTitles.MainPage;
            lbTitle.Text = WebsiteData.WebsiteData.PageInfomation.Title;
            lbBanner.Text = WebsiteData.WebsiteData.PageInfomation.Banner;
            lbBtnToSecondPage.PostBackUrl = 
            lbBtnToSecondPage.Text = 

Points of interest

Since I have to maintain a lot of information like page URLs, I try to find an easy way to reduce the work time. That's why this tool was created. Now, it's possible to write my own code without any hard coding and not having to remember the actual setting values. Just enjoy it!


  • Version 1.0 - 1/30/2009.


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


About the Author

Software Developer ChinaSoft
China China
Fighting for life.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
daiyu316623-Feb-11 20:57
memberdaiyu316623-Feb-11 20:57 
Questionhow to implement it in web application project not file system project? Pin
Member 62522816-Feb-09 18:41
memberMember 62522816-Feb-09 18:41 
AnswerRe: how to implement it in web application project not file system project? Pin
mazong112316-Feb-09 19:43
membermazong112316-Feb-09 19:43 
GeneralMy vote of 2 Pin
smortensen9-Feb-09 13:55
membersmortensen9-Feb-09 13:55 
GeneralRe: My vote of 2 Pin
mazong11239-Feb-09 15:31
membermazong11239-Feb-09 15:31 
QuestionIsn't this essentially an IoC? Pin
Russell Lear3-Feb-09 5:44
memberRussell Lear3-Feb-09 5:44 
AnswerRe: Isn't this essentially an IoC? Pin
mazong11233-Feb-09 15:34
membermazong11233-Feb-09 15:34 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 2 Feb 2009
Article Copyright 2009 by mazong1123
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid