Getting config element from ConfigurationElementCollection

I have (hopefully) setup ConfigurationElementCollection of my own design with emails as keys. Now what? Hard to find actually on the web. How do I:

  1. iterate through it?
  2. See if a specific element exists?
  3. get a specific element?


YourConfigElement config = ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;

Partial answer


foreach (X x in config.XCollection) <code here>

2 . replace "code here" with

{ if (x.Y == needle) { hasIndeed = true; break; } }

3 . replace "code here" with

{ if (x.Y == needle) cameUpWith = x; break; }

Tiny odor.

-------------Problems Reply------------

What you want is your own generic ConfigurationElementCollection base class which implements IList<T>. You can then inherit from this for all your configuration collections and cut down on the amount of work you need to do when creating configuration collections.

public abstract class BaseConfigurationElementCollection<TConfigurationElementType> : ConfigurationElementCollection, IList<TConfigurationElementType> where TConfigurationElementType : ConfigurationElement, IConfigurationElementCollectionElement, new()
protected override ConfigurationElement CreateNewElement()
return new TConfigurationElementType();

protected override object GetElementKey(ConfigurationElement element)
return ((TConfigurationElementType)element).ElementKey;

public IEnumerator<TConfigurationElement> GetEnumerator()
foreach (TConfigurationElement type in this)
yield return type;

public void Add(TConfigurationElementType configurationElement)
BaseAdd(configurationElement, true);

public void Clear()

public bool Contains(TConfigurationElementType configurationElement)
return !(IndexOf(configurationElement) < 0);

public void CopyTo(TConfigurationElementType[] array, int arrayIndex)
base.CopyTo(array, arrayIndex);

public bool Remove(TConfigurationElementType configurationElement)

return true;

bool ICollection<TConfigurationElementType>.IsReadOnly
get { return IsReadOnly(); }

public int IndexOf(TConfigurationElementType configurationElement)
return BaseIndexOf(configurationElement);

public void Insert(int index, TConfigurationElementType configurationElement)
BaseAdd(index, configurationElement);

public void RemoveAt(int index)

public TConfigurationElementType this[int index]
return (TConfigurationElementType)BaseGet(index);
if (BaseGet(index) != null)
BaseAdd(index, value);

With a little bit more work you can have a dictionary collection as well.

I don't totally understand what your issues are - but basically, if you have a custom configuration element, you should be able to retrieve that from the config file using something like:

YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;

Once you have your configuration element, you can do with it whatever you like - you can implement all those things you asked for - check existance of an element, get a specific element etc.

You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject for more information - maybe those articles will help you unlock your config "challenge" ;-)

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

Highly recommended, well written and extremely helpful!

And if you haven't discovered it already - there's an excellent Configuration Section Designer up on Codeplex which makes visually designing configuration sections and collections a snap and writes all the gooey glue code for you - very handy indeed!


Category:c# Views:2 Time:2010-05-06

Related post

  • How do I reference config elements values in other config elements? 2009-03-27

    For example, I created a provider service that uses a database. In web.config, how do I set the provider's connection string to the main application connection string, defined in <ConnectionStrings>? --------------Solutions------------- Matt's

  • Machine-variable web.config elements? 2009-09-03

    Is there a way, in the web.config, to specify machine-specific values? For example: in production I want the <customErrors> node to redirect to specific pages for displaying user-friendly error-handling pages, but in development, test, and stag

  • Can ConfigurationElementCollection object contain for its elements another ConfigurationElementCollection objects? 2010-09-26

    Can ConfigurationElementCollection object contain for its elements another ConfigurationElementCollection objects? I have this xml where testsection is section root: <testsection> <head> <metaData> <metaGroup id="general">

  • Does anyone know how to access the username and password from the Login Config Element in jMeter inside of a BeanShell? 2011-07-12

    Does anyone know how to access the username and password from the Login Config Element in jMeter inside of a BeanShell? I am trying to access the Login Config Element from and HTTPRequest also. I need to have the password saved with the dot overlay s

  • JMeter using HTTP Request Defaults config element 2012-03-02

    I see from the JMeter documentation it is possible to add parameters for HTTP Requestsin your test group: But say you have twenty threads in your test group, how do yo

  • Which .config element affects exception handling with UnhandledExceptionMode set to UnhandledExceptionMode.Automatic? 2009-02-13

    I have a Windows Forms application that has this code in the program's start up: Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic); In the MSDN Documentation for UnhandledExceptionMode.Automatic it states that: Automatic - Route

  • Replace web config *elements* with msdeploy parameters 2010-07-15

    We're using msdeploy (or web deploy if you wish) to package and deploy web apps. By declaring parameters package time we can provide values at deploy time (to replace connection strings among other things). The problem we currently face is replacing

  • How to apply two different transformations on one web.config element? 2012-01-20

    From my VS2010 deployment project I would like to apply two different transformations to two different attributes of one element in my web.config. Consider the following web.config snippet: <exampleElement attr1="false" attr2="false" attr3="true"

  • WCF Config element: baseAddressPrefixFilters 2009-02-03

    I have read the documentation for this element but still fail to understand what its purpose is. Here is a sample of how I've seen it used in examples: <serviceHostingEnvironment> <baseAddressPrefixFilters> <add prefix="http://localhos

  • .NET DLL that adds config element when being referenced 2011-05-12

    is there any possibility to automatically add a config section to the app.config/web.config from a .NET DLL when it is being referenced? Details: I created an own DLL that holds database methods. This DLL needs a custom config section, that has now t

  • How to get JMeter Include Controller to use top-test plan's config elements? 2011-10-04

    In JMeter I am attempting to create a login test plan that can be used by many other test plans. The setup is as follows: LoginTest.jmx TestPlan TestFragement PostLogin_HTTPRequest OtherTest.jmx TestPlan Thread Group HTTPRequestDefault (Contains web

  • How2 in Configuration Net 2.0: Add element to section defined in different assembly 2008-11-02

    How could I add a config element in config file in Net 2.0 that would be placed under existing structure that is defined in different assembly? In my case I have some core library that creates root section (and already some other settings) in config

  • App_Code and web.config 2009-03-23

    Let's say I have a class (MyClass.cs) in App_Code (using a ASP.NET web site, not project). There's no namespace assigned to the class. How can I get that class to be valid in the web site .aspx pages? WHAT do I need to put in the web.config file and

  • Web.Config file in 2009-03-25

    Can u tell me exact practical view of configuration section of web config here is one example <configuration> <configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Loggi

  • How to specify a WCF known type in config that is generic? 2009-06-10

    I have a type, let's call it Data<TKey>. I also have a WCF service contract that accepts a type (lets call it Wrapper) with a property of type Object (for reasons I won't go into, this isn't optional). [DataContract] public class Data<TKey

  • How to Load Config File Programmatically 2009-06-26

    Suppose I have a Custom Config File which corresponds to a Custom-defined ConfigurationSection and Config elements. These config classes are stored in a library. Config File looks like this <?xml version="1.0" encoding="utf-8" ?> <Schoool Na

  • Resources for understanding web.config essentials 2009-08-21

    What are the best resources for understanding what's possible (or not possible) to achieve using web.config settings. Wikipedia is really short on answers at this time and many of the sites I've browsed to only have one or two configurations referenc

  • web.config email query 2009-09-07

    In my web.config file there are below entries. It seems like it is used to send mails from my website. How i know maiks has been sent from my website? <add key="emailFromAddress" value="[email protected]"/> <add key="emailTo_StoreAuthorise"

  • How do you instruct NUnit to load an assembly's dll.config file from a specific directory? 2009-09-11

    If an assembly contains an app.config file, ConfigurationManager will load it as long as it is in the same directory as the NUnit project that is executing through NUnit-Gui. To illustrate consider the following folder structure. + TestFolder testPro

Copyright (C), All Rights Reserved.

processed in 0.220 (s). 11 q(s)