读的过程很容易,下面给出写入
1 /// <summary>
2 /// 修改指定配置节节点信息的值
3 /// 作 者: KidYang
4 /// 日 期: 2007-03-27
5 /// </summary>
6 /// <param name="appSettingsName">给定配置节节点</param>
7 /// <param name="newValue">目标值</param>
8 public static void WriteWebConfig(string appSettingsName, string newValue)
9 {
10 string fileName = HttpContext.Current.Server.MapPath(@"~\Web.config");
11 XmlDocument xmlDoc = new XmlDocument();
12 xmlDoc.Load(fileName);
13 XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
14 foreach (XmlElement element in topM)
15 {
16 #region 取得目标节点,并赋新值
17
18 if (element.Name == "appSettings")
19 {
20 XmlNodeList node = element.ChildNodes;
21 if (node.Count > 0)
22 {
23 foreach (XmlElement el in node)
24 {
25 if (el.Attributes["key"].Value == appSettingsName)
26 {
27 el.Attributes["value"].Value = newValue;
28 xmlDoc.Save(fileName);
29 return;
30 }
31 }
32 }
33 }
34
35 #endregion
36 }
37 }
2 /// 修改指定配置节节点信息的值
3 /// 作 者: KidYang
4 /// 日 期: 2007-03-27
5 /// </summary>
6 /// <param name="appSettingsName">给定配置节节点</param>
7 /// <param name="newValue">目标值</param>
8 public static void WriteWebConfig(string appSettingsName, string newValue)
9 {
10 string fileName = HttpContext.Current.Server.MapPath(@"~\Web.config");
11 XmlDocument xmlDoc = new XmlDocument();
12 xmlDoc.Load(fileName);
13 XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
14 foreach (XmlElement element in topM)
15 {
16 #region 取得目标节点,并赋新值
17
18 if (element.Name == "appSettings")
19 {
20 XmlNodeList node = element.ChildNodes;
21 if (node.Count > 0)
22 {
23 foreach (XmlElement el in node)
24 {
25 if (el.Attributes["key"].Value == appSettingsName)
26 {
27 el.Attributes["value"].Value = newValue;
28 xmlDoc.Save(fileName);
29 return;
30 }
31 }
32 }
33 }
34
35 #endregion
36 }
37 }