Cinchoo – Configuration framework, part 20

Include CDATA in Configuration

Here, I’m going to give you a tip on including CDATA in the configuration file. CDATA is a text data in XML that should not be parsed by the XML parser. For more information about CDATA, please visit the below link

W3Schools XML CDATA

1. Define the configuration section object decorated with ChoXmlSerializerConfigurationSectionAttribute as below. Declare a data member of type ChoCDATA.

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

    [ChoPropertyInfo("path", DefaultValue = @"C:\Log")]
    public string Path;

    [ChoPropertyInfo("commands")]
    public ChoCDATA CDATAItem = new ChoCDATA("Command1;Command2");

    #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>
  <configSections>
    <sectionGroup name="xmlSerializationSectionHandlerTest">
      <section name="applicationSettings" type="Cinchoo.Core.Configuration.ChoXmlSerializerSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <xmlSerializationSectionHandlerTest>
    <applicationSettings>
      <Path>C:\Log</Path>
      <CDATAItem><![CDATA[Command1;Command2]]></CDATAItem>
    </applicationSettings>
  </xmlSerializationSectionHandlerTest>
</configuration>
The corresponding meta-data looks as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <xmlSerializationSectionHandlerTest>
    <applicationSettings bindingMode="TwoWay" defaultable="true" silent="true">
      <configStorage>Cinchoo.Core.Configuration.ChoFileXmlSerializerConfigStorage, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de</configStorage>
      <logInfo condition="true" directory="Settings" fileName="Cinchoo.Core.Configuration.ChoXmlSerializerSectionHandlerTest.SampleConfigSection.log" />
    </applicationSettings>
  </xmlSerializationSectionHandlerTest>
</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