making the app.config sections more flexible.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QnJpYW4=?=

    making the app.config sections more flexible.

    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.configur ation 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.configur ation stuff?

    Any suggestions would be appreciated.
  • Lee

    #2
    Re: making the app.config sections more flexible.

    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" ?>
    <configuratio n>
    <appSettings>
    <add key="MyXML" value="&lt;send er>John Smith&lt;/sender>" />
    </appSettings>
    </configuration>

    And using this access function:

    static void ShowConfig()
    {

    // For read access you do not need to call OpenExeConfigur aton
    foreach (string key in ConfigurationMa nager.AppSettin gs)
    {
    string value = ConfigurationMa nager.AppSettin gs[key];
    Console.WriteLi ne("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
    ConfigurationMa nager.AppSettin gs 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...@discussi ons.microsoft.c omwrote:
    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.configur ation 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.configur ation stuff?
    >
    Any suggestions would be appreciated.

    Comment

    • Lee

      #3
      Re: making the app.config sections more flexible.

      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...@hotma il.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" ?>
      <configuratio n>
        <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 OpenExeConfigur aton
          foreach (string key in ConfigurationMa nager.AppSettin gs)
          {
              string value = ConfigurationMa nager.AppSettin gs[key];
              Console.WriteLi ne("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
      ConfigurationMa nager.AppSettin gs 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...@discussi ons.microsoft.c omwrote:
      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.configur ation 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.configur ation stuff?
      >
      Any suggestions would be appreciated.- Hide quoted text -
      >
      - Show quoted text -

      Comment

      Working...