I recently had the need to define a custom list in an application. There are many ways to make this happen but I wanted it in the app config file.
After a few searches, I pieced together the following example.
The app.config below has a custom section, AppConfigCollection.CustomDictionary. This section is declared in the configSections and the type attribute has my handler class and assembly name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="AppConfigCollection.CustomDictionary" type="AppConfigCollection.CustomDictionary, AppConfigCollection"/> </configSections> <AppConfigCollection.CustomDictionary> <NFL> <AFC> <AFC_WEST> <Item code="DEN" team="Denver Broncos" /> <Item code="MCI" team="Kansas City Chiefs" /> <Item code="SAN" team="San Diego Chargers" /> <Item code="OAK" team="Oakland Raiders" /> </AFC_WEST> </AFC> <NFC> <NFC_EAST> <Item code="DFW" team="Dallas Cowboys" /> <Item code="PHL" team="Philadelphia Eagles" /> <Item code="EWR" team="New York Giants" /> <Item code="IAD" team="Washington Redskins" /> </NFC_EAST> </NFC> </NFL> </AppConfigCollection.CustomDictionary> </configuration> |
The handler class CustomDictionary inherits from IConfigurationSectionHandler. This interface has been deprecated but it’s still available and easy to implement and make it work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Xml.Linq; namespace AppConfigCollection { class Program { static void Main(string[] args) { var teamDictionary = (Dictionary<string, string>)ConfigurationManager.GetSection("AppConfigCollection.CustomDictionary"); Console.WriteLine(); Console.WriteLine("NFL TEAMS"); foreach (var key in teamDictionary.Keys) { Console.WriteLine("{0} : {1}", key, teamDictionary[key]); } Console.WriteLine(); Console.WriteLine("press any key to continue ..."); Console.ReadLine(); } } public class CustomDictionary : IConfigurationSectionHandler { public object Create(object parent, object configContext, System.Xml.XmlNode section) { var rList = new Dictionary<string, string>(); var doc = XDocument.Parse(section.OuterXml); var root = (XElement)doc.FirstNode; var rootElement = (XElement)root.FirstNode; if (rootElement.Name != "NFL") throw new ConfigurationErrorsException( "AppConfigCollection.CustomDictionary section only accepts 'NFL' elements."); try { foreach (XElement conference in rootElement.Nodes()) { Console.WriteLine(); Console.WriteLine("Conference : {0}", conference.Name); foreach (XElement division in conference.Nodes()) { Console.WriteLine(" Division : {0}", division.Name); var teamList = from div in division.Elements("Item") select div; foreach (var item in teamList) { var code = item.Attribute("code").Value; var team = item.Attribute("team").Value; Console.WriteLine(" Team ({0}) : {1}", code, team); rList.Add(code, team); } } } } catch (Exception ex) { throw new ConfigurationErrorsException( "Error reading element.", ex); } return rList; } } } |
Feel free to download the application solution – experiment and adapt it for your needs.