Customization in .NET.

I see simple examples regarding customization in .NET. My case is a bit more complicated with nested nodes.

I would like to read this from the configuration file:

 <environments>

<environment name="live" url="http://www.live.com">
  <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">
  <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">
  <server name="b" IP="192.168.1.20"></server>
  <server name="d" IP="192.168.1.40"></server>
</environment></environments> 

If anyone could provide the code for this, I would appreciate it.

Thanks!

+3
source share
7 answers

You can read this by running your own configuration classes that inherit from the ConfigurationElement class.

Here is an example of a 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 can be implemented as follows:

public class EnvironmentElement: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return new ServerElement(...);
    }
}
+9
source

, , - CodePlex; VS.

, XML- .. .

+7

.

Phil Haack: 3 .

Edit:

Code Project, , , , :

.NET 2.0

, .

+6
+1

( ), , -, .

: , , ...

0

I would use the App.Config file

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

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

0
source

Here is what I did recently after reading this article:

Using Reflection to Bind Classes to .ini Files

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

0
source

Source: https://habr.com/ru/post/1706114/


All Articles