How To Create a Custom App.Config File With Nested Elements



TODO:

Have you ever wanted a custom config, that had nested elements in it?  For example, I need a config file like such:

 

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="EmailServiceSettings" type="Donnie.Wishard.Configuration.EmailServiceSettings, EmailServiceSettings" />
  </configSections>

  <EmailServiceSettings Name="Test Site">
    <EmailServers>
      <Server Name="Local Server" IpAddress="127.0.0.1" />
    </EmailServers>
  </EmailServiceSettings>
</configuration>

 

And to accomplish that, you need the 2 pieces of code.

 

SOLUTION:

EmailServiceSettings.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Donnie.Wishard.Configuration
{
    /// <summary>
    /// Email service settings configuation section
    /// </summary>
    public sealed class EmailServiceSettings: ConfigurationSection
    {
        #region Member Variables
        private static EmailServiceSettings settings = ConfigurationManager.GetSection("EmailServiceSettings") as EmailServiceSettings;
        #endregion
        
        #region Fields
        /// <summary>
        /// Settings Property
        /// </summary>
        public static EmailServiceSettings Settings
        {
            get { return settings; }
        }

        /// <summary>
        /// The Id
        /// </summary>
        [ConfigurationProperty("Name", DefaultValue = "", IsRequired = true)]
        public string Name
        {
            get { return(string)this["Name"]; }
            set { this["Name"] = value; }
        }
        
        /// <summary>
        /// The collection of services / methods
        /// </summary>
        [ConfigurationProperty("EmailServers", IsDefaultCollection=true, IsKey=false, IsRequired=true)]
        public EmailServersCollection EmailServers
        {
            get { return base["EmailServers"] as EmailServersCollection; }
        }
        #endregion
        #region Methods
        #endregion
    }  
    /// <summary>
    /// The EmailServersCollection class
    /// </summary>
    public sealed class EmailServersCollection : ConfigurationElementCollection
    {
        #region Member Variables
        #endregion
        #region Fields
        /// <summary>
        /// Create method
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new EmailServerElement();
        }
        /// <summary>
        /// Get element key
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return((EmailServerElement)element).Name;   //key is method name for us
        }
        /// <summary>
        /// Name element
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        new public EmailServerElement this[string name]
        {
            get { return(EmailServerElement)base.BaseGet(name); }
        }
        /// <summary>
        /// Collection type setting
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
       /// <summary>
        /// Index element
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public EmailServerElement this[int index]
        {
            get { return(EmailServerElement)base.BaseGet(index); }
        }
        /// <summary>
        /// Override the name
        /// </summary>
        protected override string ElementName
        {
            get
            {
                return "Server"; //force name of Server for elements
            }
        }
        #endregion
        #region Methods
        #endregion
    }

    /// <summary>
    /// The EmailServerElement class
    /// </summary>
    public sealed class EmailServerElement: ConfigurationElement
    {
        #region Member Variables
        #endregion
        #region Fields
        /// <summary>
        /// The Name
        /// </summary>
        [ConfigurationProperty("Name", DefaultValue = "", IsRequired = true)]
        public string Name
        {
            get { return (string)base["Name"]; }
            set { base["Name"] = value; }
        }
        /// <summary>
        /// The Method
        /// </summary>
        [ConfigurationProperty("IpAddress", DefaultValue = "", IsRequired = true)]
        public string Method
        {
            get { return (string)base["IpAddress"]; }
            set { base["IpAddress"] = value; }
        }
        #endregion
        #region Methods
        #endregion
    }
}

 

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Donnie.Wishard.Configuration
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Donnie.Wishard.Configuration.EmailServiceSettings s = Donnie.Wishard.Configuration.EmailServiceSettings.Settings;
            }
            catch (Exception x)
            {
                x = x;
            }
        }
    }
}

 

NOTES:

1.  Create a console application, and replace your Program.cs contents with the contents above.

2.  Create a class library in the solution, and put the EmailServiceSettings.cs code in it

3.  Add an app.config to the console application, using the config data from the top.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading