Saturday, September 1, 2012

.NET Simple Recursive ConfigurationElementCollections

I've seen plenty of quite complicated examples of how to load recursive configuration element collections from app.config. Here's a really simple example.

I have the app.config below. You'll see that a person can have children. Each child is also a person and so each child can have children, and so on.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
      <section name="myPeople" type="NestedCustomConfig.PeopleConfigSection, NestedCustomConfig" />
  </configSections>

  <myPeople>
    <people>
      <add name="Frank" />
      <add name="Stanley">
        <children>
          <add name="Pauline">
            <children>
              <add name="Augustus"/>
              <add name="Zebedee" />
            </children>
          </add>
        </children>
      </add>
    </people>
  </myPeople>
  
</configuration>
To load this, I have the following classes:


using System.Configuration;

namespace NestedCustomConfig
{
    public class PersonConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string) base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("children", IsDefaultCollection = false, IsRequired = false)]
        public People Children
        {
            get { return (People) base["children"]; }
            set { base["children"] = value; }
        }
    }

    public class People : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new PersonConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((PersonConfigurationElement)element).Name;
        }
    }

    public class PeopleConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("people", IsDefaultCollection = false)]
        public People People
        {
            get
            {
                return (People)base["people"];
            }
        }
    }
}
I created a Console App for the example and have the following in Program.cs:
using System.Configuration;

namespace NestedCustomConfig
{
    class Program
    {
        static void Main()
        {
            var peopleSection = ConfigurationManager.GetSection("myPeople");
        }
    }
}
Good luck.

No comments:

Post a Comment

Post a Comment