Wednesday, November 14, 2007

Create Custom Web config Section

You can build a custom section in web.config file. All you have to do is write a class that will parse that custom section. And it is up you that you may build a data structure from that.

Write the following section anywhere in you configuration node.
<configsections>
<sectiongroup name="CSettings">
<section name="CGroup" type="type name" allowLocation="true" allowDefinition="Everywhere"/>
</sectiongroup>
</configsections>

<csettings>
<cgroup>
<csection name="Shiblee" connectionstring="Password=15236545;Persist Security Info=True;User ID=sa;Initial Catalog= Shiblee;Data Source=CYCOSPS\SQL_SERVER;Timeout=86400" displayname="Shiblee">
<csubsection name="Hand">
<csection>
<cgroup>
<csettings>

Here I defined a custom section and its subsection. Now I will write a class that help to parse this XML node.
[Serializable]
public class CGroup : System.Configuration.IConfigurationSectionHandler
{
private List<csection> sections = new List<csection>();
public CGroup()
{
}
public List<csection> Sections
{
get { return sections; }
set { sections = value; }
}
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
foreach (XmlNode child in section.ChildNodes)
{
if (XmlNodeType.Element == child.NodeType)
sections.Add(new CSection(child));
}
return this;
}
public CSection this[string name]
{
get
{
foreach (CSection sec in sections)
if (sec.Name == name)
return sec;
throw new Exception("No such section define");
}
}
#endregion
}

[Serializable]
public class CSection
{
private Dictionary<string,object> data = null;
private List<csubsection> subSections = null;
public XMLRepository()
{
data = new Dictionary<string,object>();
subSections = new List<csubsection>();
}
private string this[string key]
{
get { return data.ContainsKey(key) ? data[key] : null; }
set { data[key] = value; }
}
public CSection(string name, string connStr, string displayName):this()
{
this.Name = name;
this.ConnectionString = connStr;
this.DisplayName = displayName;
}
public CSection(XmlNode sectionNode) : this()
{
this.Name = sectionNode.Attributes["name"].Value;
this.DisplayName = sectionNode.Attributes["displayName"].Value;
this.ConnectionString = sectionNode.Attributes["connectionString"].Value;
foreach (XmlNode child in repositoryNode.ChildNodes)
if (XmlNodeType.Element == child.NodeType)
this.vaults.Add(new CSubSection(child));
}
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
public string ConnectionString
{
get { return (string)this["connectionString"]; }
set { this["connectionString"] = value; }
}
public string DisplayName
{
get { return (string)this["displayName"]; }
set { this["displayName"] = value; }
}
}

Similarly we can CSubSection too. This is a pretty simple implementation. All you have to do is just implement the interface IConfigurationSectionHandler. And you will get total Xml Node and now you can access it.

System.Configuration.ConfigurationManager.GetSection("CSettings/CSection");

You will get back CGroup object, that’s it.

No comments:

Post a Comment

Please, no abusive word, no spam.