Typed lists in application configuration files
Being able to read .NET config files into typed classes has been around for ages!
So why am I writing about it? Good question!
An aspect of this which hasn’t been well covered is when we have a list of configuration items we want to load. We’ll use this app.config as an illustration:
I won’t go over the full explanation of how to use the XmlSerializerSectionHandler typed class as Craig already does a great job.
Firstly we need some C# classes (other languages are available) to represent our XML. So in single form we have:
So far, so uninteresting. The list form is thus:
The XmlRoot declaration tells the framework this is the List version of the Player class. The PlayerItems is a container for the content of the XML. Finally ensure the PlayerItems object is created correctly in the constructor.
And that’s basically it. The full and unit tested version is up on github.
Hope this helps, until next time!
Posted in : techie-stuff, tips, development, development, configuration