Click here to Skip to main content
11,573,432 members (48,095 online)
Click here to Skip to main content

Cinchoo - Support of CDATA / XML as Configuration Values

, 7 Sep 2013 CPOL 3.7K 26 3
Rate this:
Please Sign up or sign in to vote.
Consuming and Storing configuration member values as CDATA/XML

Introduction

Cinchoo is an application framework for .NET. One of the main functionalities it provides to users is the application configuration management. Application configuration is information that an application reads and/or writes at run-time from the source.

Please visit jump start article [Cinchoo - Simplified Configuration Manager] for more information about Cinchoo configuration manager.

In this section, I’m going to detail about a new feature, using and saving CDATA / XML as configuration values. So far, Cinchoo supports the configuration object member values as simple text values. It limits the configuration object member values from having any complex text.

In standard .NET framework, it is not supported in a straight way. You will have to create custom configuration section and put a plumbing code to parse the values. In Cinchoo, it is simplified to handle CDATA/XML values.

Here is how you can define configuration members to support CDATA / XML values.

How to Use

  • Download latest Cinchoo binary here.
  • Open VS.NET 2010 or higher
  • Create a sample VS.NET (.NET Framework 4) Console Application project.
  • Add reference to Cinchoo.Core.dll
  • Use the Cinchoo.Core.Configuration namespace
  • Define a configuration object 'ApplicationSettings' as below:
[ChoNameValueConfigurationSection("appSettings")]
public class AppSettings : ChoConfigurableObject
{
	[ChoPropertyInfo("name", DefaultValue = "Tom")]
	public string Name;

	[ChoPropertyInfo("address", DefaultValue="10, 
	River Road, Piscataway, NJ 08880 && New York")]
	public ChoCDATA Address;

	[ChoPropertyInfo("employer", 
	DefaultValue = "<Sample>ABCD Inc.</Sample>")]
	public string Employer;
}

In the above sample configuration object, Address member defined as ChoCDATA type to accommodate CDATA value. Employer member is defined as string type, to hold the XML value. Cinchoo automatically checks the configuration member value for any XML tags present, if it does then the section value will be created as XML element, otherwise it will be created as XML attribute.

Now let's try to create and consume the above configuration object. The below code shows how to consume and modify the object members:

static void Main(string[] args)
{
	AppSettings appSettings = new AppSettings();

	//Modify the members
	appSettings.Address = new ChoCDATA("11, Oak Road, Woodbridge, NJ 08827");
	appSettings.Employer = "<Sample1>ZZZ1 Inc.</Sample1>";

	Console.WriteLine(appSettings.ToString());
	ChoFramework.Shutdown();
}

Since Address is CDATA member, any modification must be done by creating new ChoCDATA object with new value and assign to it. And for the Employer member, it is straight forward to modify. Cinchoo framework will take care of it under the hood.

If you compile and run this sample, the output of the configuration will be seen as below:

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="appSettings" 
    type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core" />
  </configSections>
  <appSettings>
    <add key="name" value="Tom" />
    <add key="address">
      <value><![CDATA[11, Oak Road, Woodbridge, NJ 08827]]></value>
    </add>
    <add key="employer">
      <value>
        <Sample1>ZZZ1 Inc.</Sample1>
      </value>
    </add>
  </appSettings>
</configuration> 

That’s all folks, it is as easy as storing and consuming CDATA / XML values in Configuration object.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Cinchoo
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 7 Sep 2013
Article Copyright 2013 by Cinchoo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid