The shared XmlConfiguration utility contains a templated base class used to implement a basic System.Configuration.ConfigurationSection. The class simply uses an XmlSerializer for the type argument specified and deserializes while validating against an XSD file. It’s extremely easy to use as all you need is an xml serializable class to represent your configuration section. To declare the configuration section more easily I usually derive a new class to provide the type argument and use this ‘shell’ class’ type name in the configuration. Here is the most basic example:

class Config : XmlConfiguration
{
    public Config() : base() { }
}

This vastly simplifies declaring the section within the configuration file. Now embeding the section is nothing more than naming the above class:

<configsections>
    <section name="mySection" type="CSharpTest.Net.Library.Config, Library" />
</configsections>

Once this is done, simply add the root node “mySection” and populate with the xml described by your xml serializable class. The only piece missing now is the xsd, you may already have one or know how to write one. If you don’t then you can generate one by using the xsd.exe tool that comes with the .Net SDK. The convention is roughly like:

C:>"$(FrameworkSDKDir)\bin\xsd.exe" /nologo "/out:.\bin" ".\bin\Library.dll"

You can simply embed this as a resource into your application calling it with same name as the class T provided and of course keeping the .xsd extension. This should now give you a working integration and you read your configuration file easiest by simply calling “Config.ReadConfig(“mySection”);“.

Essentially this circumvents much of the system configuration mechanics and complexities to ease the development.  I frankly have always hated the System.Configuration namespace and all that it contains.  I suppose this is why I avoid it with this class.

Comments