Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads
 
Add your own
alternative version

Implement ASP.NET custom XML file configuration

, 12 Feb 2013 CPOL
We will see how to implement an ASP.NET web application and have an XML file for configuring some configuration parameters.
TestCustomXmlConfiguration-noexe.zip
packages
Microsoft.AspNet.Mvc.4.0.20710.0
lib
net40
Microsoft.AspNet.Mvc.4.0.20710.0.nupkg
Microsoft.AspNet.Razor.2.0.20710.0
lib
net40
Microsoft.AspNet.Razor.2.0.20710.0.nupkg
Microsoft.AspNet.WebApi.4.0.20710.0
Microsoft.AspNet.WebApi.4.0.20710.0.nupkg
Microsoft.AspNet.WebApi.Client.4.0.20710.0
lib
net40
Microsoft.AspNet.WebApi.Client.4.0.20710.0.nupkg
Microsoft.AspNet.WebApi.Core.4.0.20710.0
content
web.config.transform
lib
net40
Microsoft.AspNet.WebApi.Core.4.0.20710.0.nupkg
Microsoft.AspNet.WebApi.WebHost.4.0.20710.0
lib
net40
Microsoft.AspNet.WebApi.WebHost.4.0.20710.0.nupkg
Microsoft.AspNet.WebPages.2.0.20710.0
lib
net40
Microsoft.AspNet.WebPages.2.0.20710.0.nupkg
Microsoft.Net.Http.2.0.20710.0
lib
net40
net45
_._
Microsoft.Net.Http.2.0.20710.0.nupkg
Microsoft.Web.Infrastructure.1.0.0.0
Microsoft.Web.Infrastructure.1.0.0.0.nupkg
Newtonsoft.Json.4.5.6
lib
net40
Newtonsoft.Json.4.5.6.nupkg
Documentation.docx
CustomXmlConfiguration
Configs
CustomXmlConfiguration.csproj.user
Logger
obj
Debug
Properties
TestCustomXmlConfiguration.zip
System.Web.Mvc.dll
Microsoft.AspNet.Mvc.4.0.20710.0.nupkg
System.Web.Razor.dll
Microsoft.AspNet.Razor.2.0.20710.0.nupkg
Microsoft.AspNet.WebApi.4.0.20710.0.nupkg
System.Net.Http.Formatting.dll
Microsoft.AspNet.WebApi.Client.4.0.20710.0.nupkg
web.config.transform
System.Web.Http.dll
Microsoft.AspNet.WebApi.Core.4.0.20710.0.nupkg
System.Web.Http.WebHost.dll
Microsoft.AspNet.WebApi.WebHost.4.0.20710.0.nupkg
System.Web.Helpers.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll
Microsoft.AspNet.WebPages.2.0.20710.0.nupkg
System.Net.Http.dll
System.Net.Http.WebRequest.dll
_._
Microsoft.Net.Http.2.0.20710.0.nupkg
lib
net40
Microsoft.Web.Infrastructure.dll
Microsoft.Web.Infrastructure.1.0.0.0.nupkg
Newtonsoft.Json.dll
Newtonsoft.Json.4.5.6.nupkg
Documentation.docx
bin
CustomXmlConfiguration.dll
CustomXmlConfiguration.pdb
CustomXmlConfiguration.csproj.user
CustomXmlConfiguration.csprojResolveAssemblyReference.cache
CustomXmlConfiguration.dll
CustomXmlConfiguration.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using CustomXmlConfiguration.Logger;

namespace CustomXmlConfiguration.Configs
{
    /// <summary>
    /// 
    /// </summary>
    internal sealed class SettingLoader
    {
        /// <summary>
        /// Private constructor
        /// </summary>
        private SettingLoader() { }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T LoadConfig<T>() where T : class
        {
            return LoadConfig<T>( null );
        }

        /// <summary>
        /// Return Settings object from cache or from the xml file  
        /// </summary>
        /// <typeparam name="T">The type we will passing</typeparam>
        /// <param name="fileName"> </param>
        /// <returns></returns>
        ///
        public static T LoadConfig<T>(string fileName) where T : class
        {
            T configObj = null;
            try
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    fileName = HttpContext.Current.Server.MapPath
                    (string.Concat("~/", typeof(T).Name, ".config"));
                }
                string cacheKey = fileName;
                // Load the setting object from HttpRuntime Cache if the Settings.xml file is not change recently after last Chache.
                configObj = HttpRuntime.Cache[cacheKey] as T;
                if (configObj == null)// Try populate the config from cache
                {
                    configObj = LoadFromXml<T>(fileName);
                    // insert the config instance into cache use CacheDependency
                    HttpRuntime.Cache.Insert(cacheKey, configObj,new System.Web.Caching.CacheDependency(fileName));
                }
            }
            catch(Exception ex)
            {
                //write error log
                LogWriter.Instance.WriteToLog(ex.ToString());
                return null;
            }
            return configObj;
        }    
        /// <summary>
        /// Load the settings xml file and retun the Settings Type with Deserialize with xml content
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName">File Name of the custom xml Settings file</param>
        /// <returns>The T type which we have have paased with LoadFromXml<T> </returns>
        private static T LoadFromXml<T>(string fileName) where T : class
        {
            FileStream fs = null;
            try
            {
                //Serialize of the Type
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                return (T)serializer.Deserialize(fs);
            }
            catch (Exception ex)
            {
                //write error log
                LogWriter.Instance.WriteToLog(ex.ToString());
                return null;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
    }
}

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)

Share

About the Author

Md.Tazul Islam
Software Developer (Senior)
Bangladesh Bangladesh
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150326.1 | Last Updated 12 Feb 2013
Article Copyright 2013 by Md.Tazul Islam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid