Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

Understanding Section Handlers - App.config File

Rate me:
Please Sign up or sign in to vote.
4.51/5 (56 votes)
15 Jul 20054 min read 617.1K   7K   165  
This article explains configuration section handlers defined in the System.Configuration namespace and explains how to create custom sections handlers by implemeting the IConfigurationSectionHandler interface.
using System;
using System.Configuration;
using System.Xml;

namespace ConfigSections
{
	/// <summary>
	/// MyConfigHandler - Implements IConfigurationSectionHandler.
	/// This handler used to handle the companyAddress section of app.config
	/// </summary>
	public class MyConfigHandler:IConfigurationSectionHandler
	{
		public MyConfigHandler()	{	}

		#region IConfigurationSectionHandler Members

		//This is the only function defined in the interface IConfigurationSectionHandler
		public object Create(object parent, object configContext, System.Xml.XmlNode section)
		{
			 /* Parameter section will have all information about companyAddress,
			  * parst it and assign the values to CCompanyAddr object
			 */			
			CCompanyAddr objCompany=new CCompanyAddr();
			objCompany.CompanyName = section.SelectSingleNode("companyName").InnerText;
			objCompany.DoorNo = section.SelectSingleNode("doorNo").InnerText;
			objCompany.Street = section.SelectSingleNode("street").InnerText;
			objCompany.City = section.SelectSingleNode("city").InnerText;
			objCompany.PostalCode = Convert.ToInt64(section.SelectSingleNode("postalCode").InnerText);
			objCompany.Country = section.SelectSingleNode("country").InnerText;

			return objCompany;
		}

		#endregion
	}


	//CompanyAddress Class
	/// <summary>
	/// This is simple class to maintain the company address.
	/// I am using this class in MyConfigHandler
	/// </summary>
	public class CCompanyAddr
	{
		#region private variables
		private string _CompanyName;
		private string _DoorNo;
        private string _Street;
		private string _City;
		private long _PostalCode;
		private string _Country;
#endregion

		public CCompanyAddr()
		{
			_CompanyName=_DoorNo=_City=_Street=_Country="";
			_PostalCode=0;
		}
		#region Properties

		public string CompanyName
		{
			get{
				return _CompanyName;
			}
			set{
				_CompanyName = value;
			}
		}
		public string DoorNo
		{
			get{
				return _DoorNo;
			}
			set{
				_DoorNo = value;
			}
		}

		public string Street
		{
			get{
				return _Street;
			}
			set{
				_Street = value;
			}
		}

		public string City
		{
			get{
				return _City;
			}
			set{
				_City = value;
			}
		}

		public long PostalCode
		{
			get{
				return _PostalCode;
			}
			set{
				_PostalCode = value;
			}
		}

		public string Country
		{
			get{
				return _Country;
			}
			set{
				_Country = value;
			}
		}
		#endregion
	}
}

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
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions