App.config

J

John.Arthur

Hello,

I have a app.config looking like this :

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="somekey" value="somevalue" />
</appSettings>
</configuration>

And I am geting data from this fail like taht:
ConfigurationSettings.AppSettings["somekey"] ;

But I have a lot identical tag and I want to separate them in different
sub tag like this:
<?xml version="1.0"?>
<configuration>
<appSettings>
<mytag>
<add key="somekey" value="somevalue" />
</mytag>
</appSettings>
</configuration>

And the problem is that I do not know how to access the inner tag.
Can someone help me.
Thanks
 
M

Marc Scheuner [MVP ADSI]

But I have a lot identical tag and I want to separate them in different
sub tag like this:
<?xml version="1.0"?>
<configuration>
<appSettings>
<mytag>
<add key="somekey" value="somevalue" />
</mytag>
</appSettings>
</configuration>

And the problem is that I do not know how to access the inner tag.
Can someone help me.

You can do two things, basically:

1) Come up with some way to make your key names unique, and keep on
using the standard .NET configuration stuff, e.g.

<add key="mytag.somekey" value="......" />
<add key="mytag2.somekey" value="....." />

2) If that's not good enough, you will have to write your own custom
config section handler, which is really not all that crazy. All you
need to do is create a class that implements the
IConfigurationSectionHandler . That interface has just a single method
to implement, called Create, which takes an XmlNode representing your
custom config section (the whole XML from <mytag>.... all the way
through ...</mytag>). You need to write code to parse that XmlNode and
store your config settings into some suitable object - an array list,
a hashtable, or even a custom class of your own - and then returns it
to the config system.

You'll also need to add a bit to the app.config file to make your
config handler known to the .NET config system:

<configuration>
<configSections>
<section name="mytag" type=
"MyTagHandler.ConfigHandler, MyTagHandler"/>
</configSections>

and then you could use your <mytag>.....</mytag> section in the
app.config (but pay attention: this would now be OUTSIDE of
<appSettings>...</appSettings>

Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top