using System.Collections.Generic; using System.Configuration; using System.Web; namespace CallCenter.Utility { public static class Configs { //public static System.Xml.XmlDocument xDoc; //public static System.Xml.XmlNode xNode; //static Configs() //{ // xDoc = new System.Xml.XmlDocument(); // xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config")); // xNode = xDoc.SelectSingleNode("//appSettings"); //} /// /// 根据Key取Value值 /// /// public static string GetValue(string key) { try { System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config")); System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings"); var xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']"); return xElem1?.GetAttribute("value") ?? ""; } catch { return ""; } } /// /// 根据Key修改Value /// /// 要修改的Key /// 要修改为的值 public static bool SetValue(string key, string value) { try { System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config")); System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings"); //System.Xml.XmlNode xNode; System.Xml.XmlElement xElem1; System.Xml.XmlElement xElem2; xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']"); if (xElem1 != null) xElem1.SetAttribute("value", value); else { xElem2 = xDoc.CreateElement("add"); xElem2.SetAttribute("key", key); xElem2.SetAttribute("value", value); xNode.AppendChild(xElem2); } xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/system.config")); return true; } catch { return false; } } /// /// 获取配置列表 /// /// public static Dictionary GetList() { Dictionary paras = new Dictionary(); try { System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config")); System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings"); var enumer = xNode.SelectNodes("//add").GetEnumerator(); while (enumer.MoveNext()) { System.Xml.XmlElement xe = (System.Xml.XmlElement)enumer.Current; paras.Add(xe.GetAttribute("key"), xe.GetAttribute("value")); } } catch { } return paras; } } }