views:

543

answers:

7

I am seeing simple examples regarding custom configuration in .NET. My case is a bit more complex, with nested nodes.

I'd like to be able to read this from the configuration file:

 <environments>

<environment name="live" url="http://www.live.com"&gt;
  <server name="a" IP="192.168.1.10"></server>
  <server name="b" IP="192.168.1.20"></server>
  <server name="c" IP="192.168.1.30"></server>      
</environment>

<environment name="dev" url="http://www.dev.com"&gt;
  <server name="a" IP="192.168.1.10"></server>
  <server name="c" IP="192.168.1.30"></server>
</environment>

<environment name="test" url="http://www.test.com"&gt;
  <server name="b" IP="192.168.1.20"></server>
  <server name="d" IP="192.168.1.40"></server>
</environment></environments>

If anyone could provide some code for that, I'd appreciate it.

Thanks!

+7  A: 

You can read this by implementing custom configuration classes inheriting from the ConfigurationElement class.

Here is an example of the "server" element:

public class ServerElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return ((string)base["name"]); }
        set { base["name"] = value; }
    }

    ...
}

The environment element is actually a collection and could be implemented like this:

public class EnvironmentElement: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return new ServerElement(...);
    }
}
Peter Lillevold
thanks PEter, can you please go on and write the other classes? It's really confusing, how to wire-up the whole thing :/
Is this enough to get you started? :)
Peter Lillevold
Sure, thanks very much. I'll try to follow from here. Thanks again.
+1  A: 

This example might help : To create a custom configuration section handler (MSDN)

Canavar
thanks but the example doesn't have a scenario like mine... i've seen lots of simple examples, but not with nested nodes
+5  A: 

Your need to create some classes to handle the custom configuration sections of the config file.

There's a very good blog entry by Phil Haack on this: Custom Configuration Sections in 3 Easy Steps.

Edit:

I was trying to find the Code Project article I've used in the past to learn how to achieve this, and I've found it on Phil's blog entry:

here it is Unraveling the Mysteries of .NET 2.0 Configuration

it contains the info necessary to handle nested elements and collections.

Pop Catalin
Thanks but this example doesn't contain nested elements....
A: 

Looking at the content of your example (targeting different staging environments), rather than reading config files dynamically, you might want to look at web deployment projects, as indicated in the answer to an earlier question.

They handle config merging at deployment time instead: a good idea, especially if you have sensitive information like connection strings with service account passwords...

Pontus Gagge
+5  A: 

A nifty tool I came across recently is the Configuration Section Designer up on CodePlex; integrates into VS nicely.

Generates classes, XML schema, etc. Recommended.

devstuff
Excellent tool, has me going with no problems after hours of trying to get things working manually. Thanks!
phloopy
A: 

I would use App.Config file

System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];

http://www.eggheadcafe.com/community/aspnet/2/10004734/appconfig-file-c.aspx

iterationx
A: 

Here is what I implemented recently after reading this article:

Using Reflection for binding classes to .ini files

http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!174.entry

quimbo