获取Config配置值

    public static class ResourceManager
    {
        public static string GetResourceXml(string name)
        {
            var strPath = "/ResourceXML/System.config"; // 可传入
            string filename = GetMapPath2(strPath);
            XmlDocument document = new XmlDocument();
            try
            {
                document.Load(filename);
            }
            catch (Exception ex)
            {
                throw new Exception("解析xml文件出错", ex);
            }
            foreach (XmlNode node in document.SelectSingleNode("root").ChildNodes)
            {
                if ((node.NodeType != XmlNodeType.Comment) && (node.Attributes["name"].Value == name))
                {
                    return node.InnerText;
                }
            }
            return string.Empty;
        }

        private static string GetMapPath2(string strPath)
        {
            strPath = strPath.Replace("~/", "/").Replace("/", @"\");
            if (strPath.StartsWith(@"\"))
            {
                strPath = strPath.TrimStart(new char[] { '\\' });
            }
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }
    }

可以加入缓存判断

posted @ 2016-12-06 17:55  江境纣州  阅读(34)  评论(0)    收藏  举报