Home
ASP.Net
SQL Server
Web designing
C# [C Sharp]
Errors and Solutions
Others
Submit Article
Rss Feed
Xml Site Map
Contact Me
QnA
About Me

How to Create Custom Configuration Sections Using IConfigurationSectionHandler

View this article as Pdf document

Posted by amit verma on 05 Aug 2008 under Others category
Download as Pdf   

Introduction

In this post I would be taking about only IConfigurationSectionHandler, In order to create your own Custom config handler you need to have a class that implements either the IConfigurationSectionHandler interface which will force to implement the Create method. class implementation will look like this
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace MyConfigSectionHandler
{ public class MyHandler : IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
Lets say you an configuration file like this
<?xml version="1.0" encoding="utf-8"?>
<TestSkills>
<Test>
<question>Question 1</question>
</Test>
<Test>
<question>Question 2</question>
</Test>
</TestSkills>
In this you have a set of question and you want to read it and use it in your application. so lets imlement our MyHandler class
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Xml;
namespace MyConfigSectionHandler
{
public class MyHandler : IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
ArrayList aList = new ArrayList();
string question = "";
XmlNode functionNode, functionChildNode;
for (int i = 0; i < section.ChildNodes.Count; i++)
{
functionNode = section.ChildNodes[i];
if (functionNode.NodeType != XmlNodeType.Comment)
{
for (int j = 0; j < functionNode.ChildNodes.Count; j++)
{
functionChildNode = functionNode.ChildNodes[j];
if (functionChildNode.NodeType != XmlNodeType.Comment)
{
if (functionChildNode.Name.ToLower() == "question")
{
question = functionChildNode.InnerText;
}
}
aList.Add(question);
}
}
}
return aList;
}
#endregion
}
}
We would read all nodes from our config file and return it as an array list, when you implement IConfigurationSectionHandler interface and it forces you to implement Create method it will three parameter and one of them will be XmlNode which is your config file then rest is just normal xml file reading.
In order to use your hander you need to do some settings in your app.config or web.config as I'm writing the console app I will be adding these settings in a App.config, setting looks like as follows
<configSections>
<section name="TestSkills" type="MyConfigSectionHandler.MyHandler, MyConfigSectionHandler"/>
</configSections>
<TestSkills configSource="TestSkills.config"/>
In configSections we define we are have our custom handler, we give it a name and in type we define the type which is Namespace_Name.ClassName,NameSpace_Name and then we need to add a node of the name we just given to configSections which is to tell where is my config file(location)
In order to call this we saying ArrayList aList = (ArrayList)System.Configuration.ConfigurationSettings.GetConfig("TestSkills"); and then we have all values from your onfiguration file.
Download Source Code (Click here to view)

<< Previous

Next >>

About the Author

I've been workin in microsoft technologies for more then 7 years and worked on Web and windows based application.I've been working on Clasic ASP,ASP.Net,C#,Javascript,HTML,SQL server and currently exploring C#3.5 and WFC.

Enter your name

Enter your email address

Enter your comments(upto 500 chars)

Type the characters you see in the picture below.

Word Verification