making the app.config sections more flexible.

B

Brian

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.
 
L

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
 
L

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

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


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 -
 

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