There is a good article about storing lists (or any custom object) in app.config files in the Best way to save lists in App.Config
Essentially, you are creating an object that represents data.
public class MyConfig { public string[] myList; public string someOtherValueIfYouWant; }
And write a configuration handler for it ...
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; } }
And this allows you to put the following XML file in the app.config file ...
Tell app.config about your cool configuration section.
<configSections> <section name="MyConfig" type="ConfigSectionHandler,someAssembly" /> </configSection>
And then add the configuration section ...
<MyConfig> <myList>First one</myList> <myList>Second one</myList> <myList>Keep going</myList> <myList>And so on</myList> <someOtherValueIfYouWant>some non array config</someOtherValueIfYouWant> </MyConfig>
Fenton
source share