Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

Strongly Typed Custom Configuration Sections using XML Serialization

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
6 Jan 2008CPOL3 min read 62.9K   711   28  
This article demonstrates a simple approach to get strongly typed configuration objects to use in your code using XML serialization
/************************************************
 * The Citrus Framework
 * Citrus.Configuration.Demo.EmailServerSettings
 * Author: Benzi K. Ahamed
 * (C) 2007
 * **********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Citrus.Configuration
{
    /// <summary>
    /// This is a sample class that is used
    /// to describe XML serialization based configuration
    /// section usage
    /// </summary>
    [XmlRoot("EmailServerSettings")]
    public class EmailServerSettings
    {
        [XmlElement("IP")]
        public string IP { get; set; } // note the use of implicit property accessors in C# 3.0

        [XmlElement("Name")]
        public string Name { get; set; }

        #region Overrides
        public override string ToString()
        {
            return String.Format("{0} email server ({1})", Name, IP);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
I work as a Technology Lead for an IT services company based in India.

Passions include programming methodologies, compiler theory, cartooning and calligraphy.

Comments and Discussions