C#操作XML配置文件
标签:bsp add pen pat set string child create app
1 ///
2 /// 根据Key取Value值
3 ///
4 ///
命名空间:
using System.Configuration;
using System.Web;
5 public static string GetValue(string key)
6 {
7 return ConfigurationManager.AppSettings[key].ToString().Trim();
8 }
9 ///
10 /// 根据Key修改Value
11 ///
12 /// 要修改的Key
13 /// 要修改为的值
14 public static void SetValue(string key, string value)
15 {
16 System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
17 xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
18 System.Xml.XmlNode xNode;
19 System.Xml.XmlElement xElem1;
20 System.Xml.XmlElement xElem2;
21 xNode = xDoc.SelectSingleNode("//appSettings");
22
23 xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key=‘" + key + "‘]");
24 if (xElem1 != null) xElem1.SetAttribute("value", value);
25 else
26 {
27 xElem2 = xDoc.CreateElement("add");
28 xElem2.SetAttribute("key", key);
29 xElem2.SetAttribute("value", value);
30 xNode.AppendChild(xElem2);
31 }
32 xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
33 }
C#操作XML配置文件
标签:bsp add pen pat set string child create app
原文地址:https://www.cnblogs.com/lsb123/p/12491743.html
评论