Config Files and Custom Sections

  • Thread starter Thread starter Doug Ransom
  • Start date Start date
D

Doug Ransom

How can one add a section to their configuration file which allows for
whatever XML the author wants? The
System.Configuration.SingleTagSectionHandler doesn't work if I want my own
xml elements within the config file.

The config file mechanism seems really bizaree to me. I cannot understand
the advantage provided with the current format over just having an XML file
and allowing people to put in their own elements and read out what they
expect to be there, and the dotnet framework Configuration.Settings could
have just provided the file location or XML DOM Tree. If I wanted
everything to be key/value pairs, an ini file would have been good enough.
 
Doug said:
How can one add a section to their configuration file which allows for
whatever XML the author wants? The
System.Configuration.SingleTagSectionHandler doesn't work if I want
my own xml elements within the config file.

The config file mechanism seems really bizaree to me. I cannot
understand the advantage provided with the current format over just
having an XML file and allowing people to put in their own elements
and read out what they expect to be there, and the dotnet framework
Configuration.Settings could have just provided the file location or
XML DOM Tree. If I wanted everything to be key/value pairs, an ini
file would have been good enough.

Its actually quite straightforward.

1) add a <section> or a <sectionGroup> and <section> for your new XML
section in the configuration file. This can be your app config file or
machine.config
2) in <section name> give the name of your section
3) in <section type> give the full type name of a handler (ie the full name
including the namespace and the full name of the assembly)
4) create an assembly that has the handler that implements
IConfigurationSectionHandler. This class should have a method called
Create() that is called when code tries to access your section. Your code
should read the section and create an appropriate object from the data, for
example a HashTable.
5) your client code calls ConfigurationSettings.GetConfig to read the
section. If the section is nested then you give the full path within the
<configuration> node with XPath like syntax (ie separate the elements of the
path with /).

Here is a simple example that illustrates what I have said:

http://www.grimes.demon.co.uk/Downloads/CustomConfig.zip

The code creates a new section that describes a form. The section handler
reads this information, creates the form and controls on the form and
returns the form so that it can be displayed.

Richard
 
Back
Top