I'm trying to store a user list in a config file. I've been looking at DictionarySectionHandler
and NameValueSectionHandler
. I'm not too sure what the difference between these two is, but anyway, neither of them do exactly what I'd like.
You can add a custom config section like so:
<configSections>
<section name="userAges" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<userAges>
<add key="userName1" value="10" />
<add key="userName2" value="52" />
<userAges>
var userAges = (Dictionary<string,string>)ConfigurationManager
.GetSection("userAges");
foreach (var userAge in userAges)
{
userAge.Key, userAge.Value ...
}
Which is ok, but I'd prefer to be able to write:
...
<userAges>
<add user="userName1" age="10" />
<add user="userName2" age="52" />
<userAges>
...
foreach (var userAge in userAges)
{
userAge.User, userAge.Age ...
}
I've done similar things before by inheriting from ConfigElement and ConfigSection classes, but I was wondering if theres a quicker way for a simple case like this.