Click here to Skip to main content
15,892,809 members
Articles / Programming Languages / C#

The very last Configuration Section Handler I hope I'll ever need

Rate me:
Please Sign up or sign in to vote.
4.93/5 (10 votes)
27 Jun 20044 min read 85.1K   278   44  
Flexible and easy to use configuration section handler with change monitoring
using System; 
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace Haack.Configuration
{ 
	/// <summary>
	/// Flexible configuration section handler that uses XML serialization 
	/// to convert XML within a config file into an object.
	/// </summary>
	/// <remarks>
	/// <p>
	/// Usage: In your config file, use the following to map a section of 
	/// the configuration file to the handler:</p>
	/// <code>
	/// &lt;configuration&gt;
	///		&lt;configSections&gt;
	///			&lt;section name="MySection" type="Haack.Configuration.XmlSerializerSectionHandler, Haack.Configuration" /&gt;
	///		&lt;/configSections&gt;
	/// &lt;/configuration&gt;
	/// </code>
	/// <p>
	/// The section should look something like:
	/// <code>
	/// &lt;configuration&gt;
	///		&lt;MySection type="NameSpace.MyConfigSettingObject, MyAssembly"&gt;
	///			&lt;Foo&gt;1234&lt;/Foo&gt;
	///			&lt;Bar&gt;Blah&lt;/Bar&gt;
	///			&lt;Quux&gt;true&lt;/Quux&gt;
	///		&lt;/MySection&gt;
	/// &lt;/configuration&gt;
	/// </code>
	/// </p>
	/// <p>
	/// Then you can obtain the section via: </p>
	/// <code>ConfigurationSettings.GetConfig("MySection");</code>
	/// <p>
	/// And it will return an instance of the class "NameSpace.MyConfigSettingObject" 
	/// with the values from the config section.
	/// </p>
	/// <code>(MyConfigSettingObject)ConfigurationSettings.GetConfig("MySection");</code>
	/// </remarks>
	public class XmlSerializerSectionHandler : IConfigurationSectionHandler 
	{ 
		public object Create(object parent, object context, XmlNode section)
		{
			return XmlSectionSettingsBase.LoadSettings(section);
		} 
	} 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions