none
Best way of saving lists in app.config file?

    Question

  • I'm looking to save a collection of values in app.config, preferable without having to spend time getting involved with writing any special handlers.

    In straight XML, the data would look something like this:

    <xml>
       <items>
          <item name="Letter" width="8.5" height="11" value1="something" valuen="x" />
          <item name="Legal" width="8.5" height="14" value1="something" valuen="y" />
          <item name="Tabloid" width="11" height="17" value1="something" valuen="z" />
       </items>
    </xml>

    One technique I've seen is to do this:

    <appConfig>
       <add key="item1" value="Letter|8.5|11|something|x" />
       <add key="item2" value="Legal|8.5|14|something|x" />
       <add key="item3" value="Tabloid|11|17|something|x" />
    </appConfig>

    and the application would enumerate through all of the keys, paying attention to any keys beginning with "item".

    I know that I could just store the data in a entirely separate XML file.  My personal preference at the moment would be to try to keep the number of files down.

    What I really would like to do is to be able to retrieve the data in a XML-formatted string which I could use to populate a xmlDocument object.  Then I can enumerate through the data, or use SelectSingleNode and SelectNodes as nessary to extract the information. 

    I know I could just save the data in the configuration as a single string.  But, I also would prefer having to not do that as I don't want to have to try editing the string later.

    I'm just wondering if there's any other options that I'm not aware of.  Anyone have any suggestions, thoughts?

    TIA,

    Richard

     

    Tuesday, March 07, 2006 1:12 AM

Answers

  • Writing a custom config section handler is easy, elegant and probably the best option. Heres an example.

    Assume I want to store the following XML in my config file.

    <MyConfig>

    <config>someconfigdata_1</config>

    <config>someconfigdata_2</config>

    <otherconfig> some non array config</otherconfig>

    </MyConfig>

     

    The cs file for this would look like this:

    public class MyConfig

    {

    public string[] config;

    public string otherconfig;

    }

     

    You then need a configuration section handler which is a class that implements IConfigurationSectionHandler.

    public class ConfigSectionHandler : IConfigurationSectionHandler

    {

    public const string SECTION_NAME = "MyConfig";

    public object Create(object parent, object configContext, XmlNode section)

    {

    string szConfig = section.SelectSingleNode("//MyConfig").OuterXml;

    MyConfig retConf = null;

    if (szConfig != string.Empty || szConfig != null)

    {

    XmlSerializer xsw = new XmlSerializer(typeof(MyConfig));

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szConfig));

    ms.Position = 0;

    retConf = (MyConfig)xsw.DeSerialize(ms);

    }

    return retConf;

    }

    }

    Add the follwing to your App.Config file under <configuration>:

    <configuration>

    ...

    <configSections>

    <section name = "MyConfig" type = "ConfigSectionHandler,someAssembly" />

    </configSection>

    <!-- custom configuration -->

    <MyConfig>

    <config>someconfigdata_1</config>

    <config>someconfigdata_2</config>

    <config> as many as I want </config>

    <otherconfig> some non array config</otherconfig>

    </MyConfig>

    ...

    </configuration>

    Where you want to read your config you need the following code:

    MyConfig config = (MyConfig)System.Configuration.ConfigurationSettings.GetConfig("MyConfig");

     

    you can also write configuration by setting the properties and calling WriteConfiguration()

    tada! Hope this helps!

    Monday, April 03, 2006 4:27 PM

All replies

  • Writing a custom config section handler is easy, elegant and probably the best option. Heres an example.

    Assume I want to store the following XML in my config file.

    <MyConfig>

    <config>someconfigdata_1</config>

    <config>someconfigdata_2</config>

    <otherconfig> some non array config</otherconfig>

    </MyConfig>

     

    The cs file for this would look like this:

    public class MyConfig

    {

    public string[] config;

    public string otherconfig;

    }

     

    You then need a configuration section handler which is a class that implements IConfigurationSectionHandler.

    public class ConfigSectionHandler : IConfigurationSectionHandler

    {

    public const string SECTION_NAME = "MyConfig";

    public object Create(object parent, object configContext, XmlNode section)

    {

    string szConfig = section.SelectSingleNode("//MyConfig").OuterXml;

    MyConfig retConf = null;

    if (szConfig != string.Empty || szConfig != null)

    {

    XmlSerializer xsw = new XmlSerializer(typeof(MyConfig));

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szConfig));

    ms.Position = 0;

    retConf = (MyConfig)xsw.DeSerialize(ms);

    }

    return retConf;

    }

    }

    Add the follwing to your App.Config file under <configuration>:

    <configuration>

    ...

    <configSections>

    <section name = "MyConfig" type = "ConfigSectionHandler,someAssembly" />

    </configSection>

    <!-- custom configuration -->

    <MyConfig>

    <config>someconfigdata_1</config>

    <config>someconfigdata_2</config>

    <config> as many as I want </config>

    <otherconfig> some non array config</otherconfig>

    </MyConfig>

    ...

    </configuration>

    Where you want to read your config you need the following code:

    MyConfig config = (MyConfig)System.Configuration.ConfigurationSettings.GetConfig("MyConfig");

     

    you can also write configuration by setting the properties and calling WriteConfiguration()

    tada! Hope this helps!

    Monday, April 03, 2006 4:27 PM
  • To make it work... try these corrections:

    Configurationfile:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

    ...

    <configSections>

    <section name="MyConfig" type="someNamespace.ConfigSectionHandler,someAssembly" />

    </configSections>

    <!-- custom configuration -->

    <MyConfig>

    <config>

    <item>someconfigdata_1</item>

    <item>someconfigdata_2</item>

    <item>as many as I want </item>

    </config>

    <otherconfig> some non array config</otherconfig>

    </MyConfig>

    ...

    </configuration>

     

    Configuration object:

    public class MyConfig

    {

    [XmlArrayItem("item")]

    public string[] config;

    public string otherconfig;

    }

     

    Regards

    Rolf Therkildsen

    • Proposed as answer by Mr.Akshay Monday, November 05, 2012 9:09 PM
    • Unproposed as answer by Mr.Akshay Monday, November 05, 2012 9:09 PM
    • Proposed as answer by Mr.Akshay Monday, November 05, 2012 9:09 PM
    Thursday, April 20, 2006 10:14 AM