Xml使用二三事: 小事1:做一个可以动态修改的“App.config”或“web.config”

做一个可以动态修改的“App.config”或“web.config

序言:

          之前,有位同事和我讨论,他想在系统中动态去修改web.configapp.config文件的系统配置参数AppSettings[key],却发现必须要重启IISWinForm程序才能生效。我下去查下资料,发现网上介绍的方法,都是以打开xml文档的方式,去修改相应的节点。

据此,我突发奇想,为何我们不自己写一个动态的配置参数类,可以通过一个自定义的xml文件比如MySet.xml文件,来存储系统的数据库是否切换正式或测试区、数据库链接字符串、系统页面样式、页面默认查询分页单位数量、等系统其他配置参数。而且系统管理者无需远程连接到服务器上手动修改web.config文件,管理员也无需重启IIS或应用程式。

 

优点:

1、            查询速度较快:我有测试采用这种写法的查询xml文件,三

十万笔招聘公司记录的查询时间为1分钟(记录格式为:公司名称、公司类型、公司规模、公司网址、公司邮箱)。

2、            节约IO读写频率:为了减少读写配置文件xmlIO消耗,

我采用静态方法和变量存储读取文件的返回配置参数,即第一次访问时候,使用IO读取xml文件外;其他次数访问时候,则读取第一次的返回结果。

3、            实现系统配置参数的动态修改和灵活性:开发者可以根据自

己需要,定义好自己需要的xml配置参数,在程式运行中无需中断程式或IIS,即可实现配置参数的切换或修改。而且根据需要,开发者可以创建多个配置文件。

 

程序实现代码:

1、配置文件MySet.xml

 

<Config>
  <XmlDocInfo>
    <!-- 分类信息 -->
    <CatalogInfo>
      <!-- 主键 -->
      <Node ID="Catalog_ID"><![CDATA[ID]]></Node>
      <!-- 分类名称 -->
      <Node ID="Catalog_Title"><![CDATA[Title]]></Node>
      <!-- 分类描述 -->
      <Node ID="Catalog_Desc"><![CDATA[Desc]]></Node>
      <!-- 创建者 -->
      <Node ID="Catalog_Creator"><![CDATA[Creator]]></Node>
      <!-- 创建时间 -->
      <Node ID="Catalog_CreateTime"><![CDATA[CreateTime]]></Node>
      <!-- 修改人 -->
      <Node ID="Catalog_Editor"><![CDATA[Editor]]></Node>
      <!-- 修改时间 -->
      <Node ID="Catalog_EditTime"><![CDATA[EditTime]]></Node>
    </CatalogInfo>
    <!-- 链接信息 -->
    <UrlNodeInfo>
      <!-- 主键 -->
      <Node ID="UrlNode_ID"><![CDATA[ID]]></Node>
      <!-- 分类主键 -->
      <Node ID="UrlNode_CatalogID"><![CDATA[CatalogID]]></Node>
      <!-- 上级节点 -->
      <Node ID="UrlNode_ParentID"><![CDATA[ParentID]]></Node>
      <!-- 顺序编码 -->
      <Node ID="UrlNode_SortCode"><![CDATA[SortCode]]></Node>
      <!-- 链接名称 -->
      <Node ID="UrlNode_Title"><![CDATA[Title]]></Node>
      <!-- 链接描述 -->
      <Node ID="UrlNode_Desc"><![CDATA[Desc]]></Node>
      <!-- 链接地址 -->
      <Node ID="UrlNode_Url"><![CDATA[Url]]></Node>
      <!-- 创建者 -->
      <Node ID="UrlNode_Creator"><![CDATA[Creator]]></Node>
      <!-- 创建时间 -->
      <Node ID="UrlNode_CreateTime"><![CDATA[CreateTime]]></Node>
      <!-- 修改人 -->
      <Node ID="UrlNode_Editor"><![CDATA[Editor]]></Node>
      <!-- 修改时间 -->
      <Node ID="UrlNode_EditTime"><![CDATA[EditTime]]></Node>
      <!-- 备注信息 -->
      <Node ID="UrlNode_Demo"><![CDATA[Demo]]></Node>
    </UrlNodeInfo>
  </XmlDocInfo>
  <BasicSetInfo>
    <!-- 默认显示的分类信息的主键,注意:我要动态修改的一个配置参数,它存储默认查询的分类 -->
    <Node ID="MySet_DefaultCatalogID"> 366426a 8-bf0e-48b8-8293 -7a 72f 1424d98</Node>
  </BasicSetInfo>
</Config>

 

2、配置参数类

Config.cs文件里面主要有ConfigMySettings两个类,前者负责读取和修改配置参数,或者负责程序调用接口。代码如下:

 

    /// <summary>

    /// 配置文件操作类

    /// </summary>

    public class Config:BaseClass

    {

        /// <summary>

        /// 缓存配置参数

        /// </summary>

        private static Dictionary<string, string> dic = null;

 

        /// <summary>

        /// 配置文件xml的路径

        /// </summary>

        private static readonly string ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\MySet.xml";

 

        /// <summary>

        /// 获得配置信息,使用缓存数据

        /// </summary>

        /// <param name="key"></param>

        /// <param name="catalog"></param>

        /// <returns></returns>

        public string this[string fieldName, string nodePath]

        {

            get

            {// 读取配置参数

                string result = "";

                if(dic != null && dic.Count > 0)

                {

                    if(dic.Keys.Contains(fieldName))

                    {

                        result = dic[fieldName];

                    }

                    else

                    {

                        result = this.GetValue(fieldName, nodePath);

                        dic.Add(fieldName, result);

                    }

                }

                else

                {

                    if(dic == null)

                    {

                        dic = new Dictionary<string,string>();

                    }

 

                    result = this.GetValue(fieldName, nodePath);

                    dic.Add(fieldName, result);

                }

                return result;

            }

            set

            {// 修改配置参数

                this.SetValue(fieldName, nodePath, value);

            }

        }

 

        /// <summary>

        /// 查询配置节点的参数

        /// </summary>

        /// <param name="fieldName">字段名称</param>

        /// <param name="nodePath">字段路径</param>

        /// <returns></returns>

        private string GetValue(string fieldName, string nodePath)

        {

            string result = "";

            XmlDocument xdoc = null;

            XmlNode node = null;

            if (!File.Exists(ConfigPath))

            {

                throw new Exception("Config\\MySet.config is not exsist");

            }

 

            try

            {

                xdoc = new XmlDocument();

                xdoc.Load(ConfigPath);

                if(xdoc != null)

                {

                    // node = xdoc.SelectSingleNode(string.Format("//Config/BaseInfo/Node[@id='{0}' and @Enable = '1']", fieldName));

                    node = xdoc.SelectSingleNode(string.Format("{0}[@ID = '{1}']", nodePath, fieldName));

                    if(node != null)

                    {

                        result = node.InnerText;

                    }

                }

            }

            catch(Exception ex)

            {

                throw ex;

            }

            finally

            {

                xdoc = null;

                node = null;

            }

            return result;

        }

 

        /// <summary>

        /// 修改配置节点的参数

        /// </summary>

        /// <param name="fieldName">字段名称</param>

        /// <param name="nodePath">字段路径</param>

        /// <returns></returns>

        private bool SetValue(string fieldName, string nodePath, string fieldValue)

        {

            bool result = false;

            XmlDocument xdoc = null;

            XmlNode node = null;

            if (!File.Exists(ConfigPath))

            {

                throw new Exception("Config\\WebPartMaker.config is not exsist");

            }

 

            try

            {

                xdoc = new XmlDocument();

                xdoc.Load(ConfigPath);

                if(xdoc != null)

                {

                    // node = xdoc.SelectSingleNode(string.Format("//Config/BaseInfo/Node[@id='{0}' and @Enable = '1']", fieldName));

                    node = xdoc.SelectSingleNode(string.Format("{0}[@ID='{1}']", nodePath, fieldName));

                    if(node != null)

                    {

                        node.InnerText = fieldValue;

                        xdoc.Save(ConfigPath);

                        result = true;

                    }

                }

            }

            catch(Exception ex)

            {

                throw ex;

            }

            finally

            {

                xdoc = null;

                node = null;

            }

            return result;

        }

 

}

 

    /// <summary>

    /// 系统配置参数工具类

    /// </summary>

    public static class MySettings

    {

        private static Config myConfig = new Config(); 

 

        #region XmlDoc Parameters

 

        /// <summary>

        /// XmlData存放路径

        /// </summary>

        public static readonly string XmlDataFpath = System.AppDomain.CurrentDomain.BaseDirectory + "XmlData\\";

 

        #region Catalog.xml Fields

 

        /// <summary>

        /// Catalog.xml文件路径

        /// </summary>

        public static readonly string CatalogXmlFpath = XmlDataFpath + "Catalog.xml";

 

        /// <summary>

        /// 分类信息xml节点路径

        /// </summary>

        private static readonly string CatalogInfoPath = "//Config/XmlDocInfo/CatalogInfo/Node";

 

        /// <summary>

        /// 主键

        /// </summary>

        public static readonly string Catalog_ID = myConfig["Catalog_ID", CatalogInfoPath];

 

        /// <summary>

        /// 分类名称

        /// </summary>

        public static readonly string Catalog_Title = myConfig["Catalog_Title", CatalogInfoPath];

 

        /// <summary>

        /// 分类描述

        /// </summary>

        public static readonly string Catalog_Desc = myConfig["Catalog_Desc", CatalogInfoPath];

 

        /// <summary>

        /// 创建者

        /// </summary>

        public static readonly string Catalog_Creator = myConfig["Catalog_Creator", CatalogInfoPath];

 

        /// <summary>

        /// 创建时间

        /// </summary>

        public static readonly string Catalog_CreateTime = myConfig["Catalog_CreateTime", CatalogInfoPath];

 

        /// <summary>

        /// 修改人

        /// </summary>

        public static readonly string Catalog_Editor = myConfig["Catalog_Editor", CatalogInfoPath];

 

        /// <summary>

        /// 修改时间

        /// </summary>

        public static readonly string Catalog_EditTime = myConfig["Catalog_EditTime", CatalogInfoPath];

 

        #endregion

 

        #region UrlNode.xml Fields

 

        /// <summary>

        /// UrlNode.xml文件路径

        /// </summary>

        public static readonly string UrlNodeXmlFpath = XmlDataFpath + "UrlNode.xml";

 

        /// <summary>

        /// 分类信息xml节点路径

        /// </summary>

        private static readonly string UrlNodeInfoPath = "//Config/XmlDocInfo/UrlNodeInfo/Node";

 

        /// <summary>

        /// 主键

        /// </summary>

        public static readonly string UrlNode_ID = myConfig["UrlNode_ID", UrlNodeInfoPath];

 

        /// <summary>

        /// 分类主键

        /// </summary>

        public static readonly string UrlNode_CatalogID = myConfig["UrlNode_CatalogID", UrlNodeInfoPath];

 

        /// <summary>

        /// 上级节点

        /// </summary>

        public static readonly string UrlNode_ParentID = myConfig["UrlNode_ParentID", UrlNodeInfoPath];

 

        /// <summary>

        /// 顺序编码

        /// </summary>

        public static readonly string UrlNode_SortCode = myConfig["UrlNode_SortCode", UrlNodeInfoPath];

 

        /// <summary>

        /// 链接名称

        /// </summary>

        public static readonly string UrlNode_Title = myConfig["UrlNode_Title", UrlNodeInfoPath];

 

        /// <summary>

        /// 链接描述

        /// </summary>

        public static readonly string UrlNode_Desc = myConfig["UrlNode_Desc", UrlNodeInfoPath];

 

        /// <summary>

        /// 链接地址

        /// </summary>

        public static readonly string UrlNode_Url = myConfig["UrlNode_Url", UrlNodeInfoPath];

 

        /// <summary>

        /// 创建者

        /// </summary>

        public static readonly string UrlNode_Creator = myConfig["UrlNode_Creator", UrlNodeInfoPath];

 

        /// <summary>

        /// 创建时间

        /// </summary>

        public static readonly string UrlNode_CreateTime = myConfig["UrlNode_CreateTime", UrlNodeInfoPath];

 

        /// <summary>

        /// 修改人

        /// </summary>

        public static readonly string UrlNode_Editor = myConfig["UrlNode_Editor", UrlNodeInfoPath];

 

        /// <summary>

        /// 修改时间

        /// </summary>

        public static readonly string UrlNode_EditTime = myConfig["UrlNode_EditTime", UrlNodeInfoPath];

 

        /// <summary>

        /// 备注信息

        /// </summary>

        public static readonly string UrlNode_Demo = myConfig["UrlNode_Demo", UrlNodeInfoPath];

 

        #endregion

 

        #endregion

 

        #region Config Parameters

 

        /// <summary>

        /// Config存放路径

        /// </summary>

        public static readonly string ConfigFpath = AppDomain.CurrentDomain.BaseDirectory + "Config\\";

 

        #region MySet.xml Fields

 

        /// <summary>

        /// MySet.xml存放路径

        /// </summary>

        public static readonly string MySetXmlFpath = ConfigFpath + "MySet.xml";

 

        /// <summary>

        /// 分类信息xml节点路径

        /// </summary>

        private static readonly string MySetInfoPath = "//Config/BasicSetInfo/Node";

 

        /// <summary>

        /// 默认显示的分类信息的主键

        /// </summary>

        public static string MySet_DefaultCatalogID

        {

            get { return myConfig["MySet_DefaultCatalogID", MySetInfoPath]; }

            set { myConfig["MySet_DefaultCatalogID", MySetInfoPath] = value; }

        }

        #endregion

        #endregion

 

        #region Project Settings

        /// <summary>

        /// 长字符时间格式, yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static readonly string TimeLong24HHFormat = "yyyy/MM/dd HH:mm:ss";

        #endregion

    }

你可能感兴趣的:(config)