Cinchoo – Configuration framework, part 12

Using ChoFileXmlSerializerConfigStorage

Download sample source files (Require .NET 4.0 / Visual Studio 2010)

In this section, I’ll go over the details of using ChoFileXmlSerializerConfigStorage in your application. It’s a custom configuration section handler enables application to read and write application settings from configuration file in complex Xml format.

1. Define the configuration section object decorated with ChoXmlSerializerConfigurationSectionAttribute as below.

[ChoXmlSerializerConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
    #region Instance Data Members (Public)

    [XmlAttribute]
    public string Name;

    [XmlElement]
    public string Message;

    #endregion

    [ChoAfterConfigurationObjectLoadedHandler]
    void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
    {
        Console.WriteLine(sender.ToString());
    }
}

2. Now instantiate and use it as below.

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample Name="Mark">
    <Message>Hello World!</Message>
  </sample>
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoXmlSerializerSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
</configuration>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s