Web Service: XML Config To C# Class

Search

 by Remas Wojciechowski

April 4th, 2002
Change protocol: April 22nd, 2002 - added link to Daniel Olson's article.

Learn about .NET configuration and use this web services to automatically create a wrapper class for handling configuration data.

Introduction

.NET offers an elegant way to store configuration data for applications the .config file. You can use that mechanism in both Windows and ASP.NET applications.

Configuration Files

The .config file contains numerous configuration sections. The developer can add custom application data by means of the <appSettings> section. In that section parameters can be defined with the <add /> element where the parameter name and value are set by means of the key and value attributes respectively. Here's an example of a .config file that sets the value Bar to the parameter Foo.

Example of a .config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<appSettings>
		<add key="Foo" value="Bar" />
	</appSettings>
</configuration>

The access to the parameters defined in the .config file is provided by the System.Configuration.ConfigurationSettings class and specifically its static property AppSettings. The code snippet below presents how value of the Foo paremeter for the configuration file above can be accessed:

Accessing configuration settings.
System.Configuration.ConfigurationSettings.AppSettings["Foo"]

For this to work, the .config file must meet certain requirements. These are different for Windows and ASP.NET applications:

Windows Applications
The file must be named [appname].config, where appname is the full name of the application's executable. For example, given that the executable name is goodapp.exe, the .config file must be named goodapp.exe.config. Also, the .config file must be saved in the same directory as the executable.
ASP.NET Applications
The file must be named web.config and saved in the root of the ASP.NET application.
Read more about .NET configuration on MSDN:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconconfiguringnetframeworkapplications.asp

Configuration Class

The .NET configuration files are a nifty feature. However, it's, in my opinion, not very elegant to use ConfigurationSettings.AppSettings throughout your application. Firstly, you need to check if the parameter you're trying to access is present (i.e. not null). Secondly, AppSettings returns strings regardless of what type your parameter actually is. That's why I decided to create a wrapper class that offers strongly typed properties that correspond to the configuration parameters and offers some usefull methods on top of that.

Here's the corresponding class for the sample configuration file above.

Sample configuration class.
using System;
using System.Configuration;
using System.Text;
namespace AutoConfig 
{
	public class Config 
	{
		public string Foo 
		{
			get 
			{
				return _Foo;
			}
			set 
			{
				_Foo = value;
			}
		}
		public void Load() 
		{
			if (ConfigurationSettings.AppSettings["Foo"] != null) 
			{
				_Foo = ConfigurationSettings.AppSettings["Foo"].ToString();
			}
		}
		public override string ToString() 
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("Foo={0}", _Foo.ToString());
			sb.Append("\n");

			return sb.ToString();
		}
		private string _Foo;
	}
}

As you can see the class offers a public properties for each configuration parameter. In order to populate the class you can use the Load method. That method checks whether the parameters exist. Moreover, the class overrides the ToString method. The overriden version returns the values of all properties, a feature usefull for monitoring and debugging.

Daniel Olson suggests a slightly different approach to a wrapper class.
http://www.c-sharpcorner.com/Code/2002/April/XMLConfigInWinForms.asp

The XML Config To C# Class Web Service

You'll probably agree that the wrapper class offers some advantages. However, you'll need to update the class anytime you change the .config file. After a couple of such changes I grew tired of constantly updating the config class and wrote a web service that creates the wrapper class for a given configuration XML document. In this version the web service assumes that all parameters are of type string so some editing is required.

I'm already thinking of a nifty way to make the web service more type-sensitive. If you have any spontaneous zinger ideas, let me know!

If you like the idea of the wrapper class, feel free to use that web service--as a matter of fact, feel free to use it even if you don't like the wrapper class. :)

XML Config To C# Class Web Service
http://www.aspalliance.com/remas/ASP.NET/WebServices/XMLConfigToClass/Config.asmx

Note: If you use the web frontend of the web service, your XML configuration string must not contain any newline charasters.