Click here to Skip to main content
15,896,442 members
Articles / Programming Languages / XML

Custom AppSettings

Rate me:
Please Sign up or sign in to vote.
4.12/5 (7 votes)
29 Nov 2005CPOL5 min read 84.8K   1.9K   21  
Another article on AppSettings.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;

namespace BellAppSetting
{
	/// <summary>
	/// AppSettingHandler is a class that allows you to read customized application configuration
	/// </summary>
	public class AppSettingHandler : IConfigurationSectionHandler
	{
		public AppSettingHandler()
		{
			
		}


		/// <summary>
		/// This method implements the IConfigurationSectionHandler interface.
		/// This interface defines the contract that all configuration section handlers must 
		/// implement in order to participate in the resolution of configuration settings.
		/// </summary>
		/// <param name="parent">
		/// The configuration settings in a corresponding parent configuration section.
		/// </param>
		/// <param name="configContext">
		/// An HttpConfigurationContext when Create is called from the ASP.NET configuration system. 
		/// Otherwise, this parameter is reserved and is a null reference
		/// </param>
		/// <param name="section">
		/// The XmlNode that contains the configuration information from the configuration file. 
		/// Provides direct access to the XML contents of the configuration section.
		/// </param>
		/// <returns>
		/// In this case it returns a complex ArrayList, its structure is detailed in the App.config
		/// of the sample project CApp
		/// </returns>
		/// <remarks>
		/// If everything is correct, that it in the calling code and in the App.config
		/// .Net will automaticaly call this method, and pass the content as the section XmlNode 
		/// to process it.
		/// </remarks>
		public virtual object Create(object parent, object configContext, XmlNode section)
		{
			string temp;
			Hashtable ConfigChilds = new Hashtable();
			ArrayList AllElements = new ArrayList();
			ArrayList Sections = new ArrayList();
			ArrayList innerSectionElements = new ArrayList();
			XmlAttribute xmlatt;
			XmlNodeList xmlnlist1, xmlnlist2;
			//We look for the Config nodes
			xmlnlist1 = section.SelectNodes("Configs");
			foreach (XmlNode ConfigNode in xmlnlist1) 
			{ //This foreach goes threw each Configs Node 
				xmlatt = ConfigNode.Attributes["use"]; //We read the attibute 'use'
				if (!bool.Parse(xmlatt.Value)) 
				{ //If it says <Configs use="false">, we skip it
					continue;
				}
				else if (ConfigNode.HasChildNodes) //If the node has children, we go threw each one
				{
					foreach (XmlNode ConfigChildElement in ConfigNode.ChildNodes)
					{ //This foreach goes threw each Config Child Element
						if ((ConfigChildElement.NodeType == XmlNodeType.Whitespace) || (ConfigChildElement.NodeType == XmlNodeType.Comment) )
							continue;
						else
						{
							temp = ConfigChildElement.Attributes["key"].Value;
							xmlatt = ConfigChildElement.Attributes["use"]; //We read the attibute 'use'
							if (bool.Parse(xmlatt.Value) && !ConfigChilds.Contains(temp)) 
							{ //If it says <add key="SubSection" use="true" />, we parse it
								xmlnlist2 = section.SelectNodes(temp);
								if (xmlnlist2.Count != 0) //If the node has sections, we go threw each one
								{
									foreach (XmlNode ChildNode in xmlnlist2)
									{//This foreach goes threw each Config Child Node
										if ((ChildNode.NodeType == XmlNodeType.Whitespace) || (ChildNode.NodeType == XmlNodeType.Comment) )
											continue;
										else
										{
											if (bool.Parse(ChildNode.Attributes["use"].Value))
											{
												foreach (XmlNode ChildElement in ChildNode)
												{//This foreach goes threw each Config Child Node Elements
													if ((ChildElement.NodeType == XmlNodeType.Whitespace) || (ChildElement.NodeType == XmlNodeType.Comment) )
														continue;
													else
													{
														innerSectionElements.Add(new DictionaryEntry(ChildElement.Attributes["key"].Value, ChildElement.Attributes["value"].Value));
													}
												}
												Sections.Add(innerSectionElements.Clone());
												innerSectionElements.Clear();
											}
										}
									}
									ConfigChilds.Add(temp, Sections.Clone());
									Sections.Clear();
								}
							}
						}
					}
					AllElements.Add(ConfigChilds.Clone());
					ConfigChilds.Clear();
				}
			}
			return AllElements;
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Althought i'm a Electronic Engineering, my stong side is IT.

I've developped code in different languages:
Assembly, C, Java, HTML, Javascript, Perl/CGI, VB6, and C#, in which i've been working latetly.

I'm also keen on Security and Cryptography topics, as well as on Multimedia Technologies.

Comments and Discussions