By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

making the app.config sections more flexible.

P: n/a
I have an application that is responsible for retrieving data from a variety
of remote sites using all sorts of different protocols. e.g. email, web
service, ftp, screen scraping etc. I want to convert the app.config file from
having tons of settings in the appSettings section of the file and have a
section for each of the methods/locations I need to access. However, I don't
really want to have different sections for email, ftp etc. I want to be able
to have one common section type that has enough information for me to know
which class to invoke to do the work, then I want to be able to pass that
class an xml fragment that would vary from protocol to protocol etc. and let
it configure itself without accessing the app.config file directly and
without the higher level portion having to know what the various xml child
fragments mean.

the problem is I don't know if it is possible to easily configure the thing
so that i can use the system.configuration calls to retrieve xml fragments as
opposed to objects or is the only way to directly suck up the app.config as
an xml document myself and not use the system.configuration stuff?

Any suggestions would be appreciated.
Oct 21 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Lee
Brian,

If I understand your request, you want to have XML "chunks" inside the
app.config file that can be read as simple strings, right?

Using this app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyXML" value="&lt;sender>John Smith&lt;/sender>" />
</appSettings>
</configuration>

And using this access function:

static void ShowConfig()
{

// For read access you do not need to call OpenExeConfiguraton
foreach (string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
}

I get this string as output:

Key: MyXML, Value: <sender>John Smith</sender>

The key is that I used &lt; instead of < then you can put as much XML
inside of the app.config file as you want. the
ConfigurationManager.AppSettings automatically XML decodes the &lt; to
< symbols. You can verify this by adding a breakpoint after the
variable named value is assigned the XML string. It shows in the text
visualizer that it is indeed < not &lt;

Is this what you were looking for? If not, please elaborate and I'll
try to answer your question better.

L. Lee Saunders
my blog - http://oldschooldotnet.blogspot.com

On Oct 21, 10:39*am, Brian <Br...@discussions.microsoft.comwrote:
I have an application that is responsible for retrieving data from a variety
of remote sites using all sorts of different protocols. e.g. email, web
service, ftp, screen scraping etc. I want to convert the app.config file from
having tons of settings in the appSettings section of the file and have a
section for each of the methods/locations I need to access. However, I don't
really want to have different sections for email, ftp etc. I want to be able
to have one common section type that has enough information for me to know
which class to invoke to do the work, then I want to be able to pass that
class an xml fragment that would vary from protocol to protocol etc. and let
it configure itself without accessing the app.config file directly and
without the higher level portion having to know what the various xml child
fragments mean.

the problem is I don't know if it is possible to easily configure the thing
so that i can use the system.configuration calls to retrieve xml fragments as
opposed to objects or is the only way to directly suck up the app.config as
an xml document myself and not use the system.configuration stuff?

Any suggestions would be appreciated.
Oct 21 '08 #2

P: n/a
Lee
I see that my XML was altered it my previous post. The app.config
file contained & l t ; (no spaces) which were changed to < when I
posted my reply. Sorry.

So, <add key="MyXML" value="<sender>John Smith</sender>" / should
read <add key="MyXML" value="& lt;sender>John Smith& lt;/sender>" />
(Again no spaces)

L. Lee Saunders
my blog - http://oldschooldotnet.blogspot.com

On Oct 21, 4:05*pm, Lee <saund...@hotmail.comwrote:
Brian,

If I understand your request, you want to have XML "chunks" inside the
app.config file that can be read as simple strings, right?

Using this app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
* <appSettings>
* * <add key="MyXML" value="<sender>John Smith</sender>" />
* </appSettings>
</configuration>

And using this access function:

static void ShowConfig()
*{

* * // For read access you do not need to call OpenExeConfiguraton
* * foreach (string key in ConfigurationManager.AppSettings)
* * {
* * * * string value = ConfigurationManager.AppSettings[key];
* * * * Console.WriteLine("Key: {0}, Value: {1}", key, value);
* * *}
*}

I get this string as output:

Key: MyXML, Value: <sender>John Smith</sender>

The key is that I used < instead of < then you can put as much XML
inside of the app.config file as you want. the
ConfigurationManager.AppSettings automatically XML decodes the < to
< symbols. *You can verify this by adding a breakpoint after the
variable named value is assigned the XML string. *It shows in the text
visualizer that it is indeed < not <

Is this what you were looking for? *If not, please elaborate and I'll
try to answer your question better.

L. Lee Saunders
my blog -http://oldschooldotnet.blogspot.com
On Oct 21, 10:39*am, Brian <Br...@discussions.microsoft.comwrote:
I have an application that is responsible for retrieving data from a variety
of remote sites using all sorts of different protocols. e.g. email, web
service, ftp, screen scraping etc. I want to convert the app.config file from
having tons of settings in the appSettings section of the file and havea
section for each of the methods/locations I need to access. However, I don't
really want to have different sections for email, ftp etc. I want to beable
to have one common section type that has enough information for me to know
which class to invoke to do the work, then I want to be able to pass that
class an xml fragment that would vary from protocol to protocol etc. and let
it configure itself without accessing the app.config file directly and
without the higher level portion having to know what the various xml child
fragments mean.
the problem is I don't know if it is possible to easily configure the thing
so that i can use the system.configuration calls to retrieve xml fragments as
opposed to objects or is the only way to directly suck up the app.config as
an xml document myself and not use the system.configuration stuff?
Any suggestions would be appreciated.- Hide quoted text -

- Show quoted text -
Oct 21 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.