.net中webform,winform读取xml配置文件的方法

来源:互联网 发布:ubuntu sudo 编辑:程序博客网 时间:2024/06/11 16:29

webform读取:

string.Format(SqlConfig.GetSql("SQLServer.QueryTableInfo"), "SysLog");

/// <summary>
        /// 读取指定SQL
        /// </summary>
        /// <param name="key">sql名称</param>
        /// <returns>sql字符串</returns>
        public static string GetSql(string key)
        {
            string sql = string.Empty;
            try
            {
                string[] sqlLayers = key.Split('.');
                XmlNode xmlNode = ReadMapping(sqlLayers[0]);
                sql = xmlNode.SelectNodes("SqlMaps[@key='" + sqlLayers[1] + "']/SqlMap[@key='" + sqlLayers[2] + "']")[0].InnerText;
            }
            catch (Exception ex)
            {
                DataLib.LogAccess.Log("获取SQL:" + key + "错误:" + ex.ToString());
            }
            return sql;
        }

        /// <summary>
        /// 读取配置文件中的Mapping节点
        /// </summary>
        /// <param name="key">mapping节点名称</param>
        /// <returns>Mapping节点下的sqlMap</returns>
        private static XmlNode ReadMapping(string key)
        {
            XmlDocument Xdoc;
            string fileName = HttpContext.Current.Server.MapPath("~/mapping.config");
            //if (HttpContext.Current.Cache["mapping"] == null)
            //{
                Xdoc = new XmlDocument();
                Xdoc.Load(fileName);
                HttpContext.Current.Cache.Add("mapping", Xdoc, new System.Web.Caching.CacheDependency(fileName),
                    DateTime.Now.AddDays(3), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
            //}
            //else
                Xdoc = (XmlDocument)HttpContext.Current.Cache["mapping"];
            return Xdoc.SelectSingleNode("/configuration/mapping[@key='" + key + "']");
        }
    }

 

winfrom读取

string.Format(SqlConfig.GetSql("SQLServer.QueryTableInfo"), "SysLog");

 /// <summary>
        /// 读取指定SQL
        /// </summary>
        /// <param name="key">sql名称</param>
        /// <returns>sql字符串</returns>
        public static string GetSql(string key)
        {
            string sql = string.Empty;
            try
            {
                string[] sqlLayers = key.Split('.');
                XmlNode xmlNode = ReadMapping(sqlLayers[0]);
                XmlNodeList list = xmlNode.SelectNodes("Sql");
                foreach (XmlNode node in list)
                {
                    if (node.Attributes["key"].Value.Equals(sqlLayers[1]))
                    {
                        return node.InnerText;
                    }
                }
                sql = xmlNode.SelectNodes("SqlMap[@key='" + sqlLayers[1] + "']")[0].InnerText;
            }
            catch (Exception ex)
            {
                DataLib.LogAccess.Log("获取SQL:" + key + "错误:" + ex.ToString());
            }
            return sql;
        }

        /// <summary>
        /// 读取配置文件中的Mapping节点
        /// </summary>
        /// <param name="key">mapping节点名称</param>
        /// <returns>Mapping节点下的sqlMap</returns>
        private static XmlNode ReadMapping(string key)
        {
            XmlDocument Xdoc;
            string fileName = System.Windows.Forms.Application.StartupPath + (@"\SqlMapping.config");
            Xdoc = new XmlDocument();
            Xdoc.Load(fileName);
            XmlNodeList list = Xdoc.SelectNodes("SqlMapping/SqlMaps");
            foreach (XmlNode node in list)
            {
                if (node.Attributes["key"].Value.Equals(key))
                {
                    return node;
                }
            }
            return null;
        }
    }

<?xml version="1.0" encoding="utf-8" ?>
<SqlMapping>
  <SqlMaps key="SQLServer">
    <Sql key="QueryAllTable">
      <![CDATA[
        select name from sysobjects where type='U' and name <>'sysdiagrams' order by name asc
      ]]>
    </Sql>

  </SqlMaps>

</SqlMapping>