Let says you want to store your application settings in an App.config or Web.config file and these settings should contain collections of settings, then use can use the following code to deserialize an object containing these settings from an App.config or Web.config. By using deserialization the object can contain any type that’s serializable.

 

The App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ApplicationSettings" type="Rvl.NewCode.Test.BC.ApplicationSettings, Rvl.NewCode.Test" />
  </configSections>
  <ApplicationSettings>
    <Id>12</Id>
    <CarTypes>
      <string>Audi</string>
      <string>BMW</string>
      <string>Mercedes</string>
    </CarTypes>
  </ApplicationSettings>
</configuration>

 

The code to deserialize the object from the App.config file

ApplicationSettings settings = new ApplicationSettings();
settings.InitializeFromAppConfig();

After running this code the settings object, contains the data:

image

 

The ApplicationSettings class

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Rvl.NewCode.Test.BC
{
    public class ApplicationSettings : IConfigurationSectionHandler
    {
        public int Id { get; set; }
        public List<string> CarTypes { get; set; }
        /// <summary>
        /// Creates a configuration section handler.
        /// </summary>
        /// <param name="parent">Parent object.</param>
        /// <param name="configContext">Configuration context object.</param>
        /// <param name="section">Section XML node.</param>
        /// <returns>
        /// The created section handler object.
        /// </returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            return section;
        }
        /// <summary>
        /// Initialize this object from app config.
        /// </summary>
        public void InitializeFromAppConfig()
        {
            XmlElement settingsXmlElement       = GetXmlElementFromAppConfig();
            ApplicationSettings settings    = DeserializeFromXmlElement(settingsXmlElement);
            CopyDataToCurrentObject(settings);
        }
        /// <summary>
        /// Gets the XML element from app config.
        /// </summary>
        /// <returns>
        /// The found xml element.
        /// </returns>
        private XmlElement GetXmlElementFromAppConfig()
        {
            string tagName = this.GetType().Name;
            XmlElement sectionElement = ConfigurationManager.GetSection(tagName) as XmlElement;
            if (sectionElement == null)
            {
                throw new ApplicationException(string.Format("Could not find section [{0}] in the App.config file.", tagName));
            }
            return sectionElement;
        }
        /// <summary>
        /// Convert the given XML element to a ScheduleServiceSettings object.
        /// </summary>
        /// <param name="sectionElement">The XML element containing the information to create a ScheduleServiceSettings object.</param>
        /// <returns>A ScheduleServiceSettings object</returns>
        private ApplicationSettings DeserializeFromXmlElement(XmlElement sectionElement)
        {
            ApplicationSettings settings = null;
            using (StringReader reader = new StringReader(sectionElement.OuterXml))
            {
                XmlSerializer xs    = new XmlSerializer(typeof(ApplicationSettings));
                settings            = xs.Deserialize(reader) as ApplicationSettings;
            }
            return settings;
        }
        /// <summary>
        /// Copy the data found in the ScheduleServiceSettings object to the current object
        /// </summary>
        /// <param name="settings"></param>
        private void CopyDataToCurrentObject(ApplicationSettings settings)
        {
            Id = settings.Id;
            CarTypes = settings.CarTypes;
        }
    }
}

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.