1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Xml; //引入的是命名空间
12public partial class Default3 : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!Page.IsPostBack)
17 {
18 this.read();
19 }
20 }
21 /// <summary>
22 /// 读取Web.Config中的节点
23 /// </summary>
24 public void read()
25 {
26 string fileName = Server.MapPath("") + @"\Web.Config";
27
28 XmlDocument xmlDoc = new XmlDocument();//表示的是Xml的文档
29
30 xmlDoc.Load(fileName);//加载Xml文档
31
32 XmlNodeList xmlNode = xmlDoc.DocumentElement.ChildNodes;//获取文档节点下的所有的子节点
33
34 foreach (XmlElement element in xmlNode)//在当前的节点中循环查找
35 {
36 if (element.Name == "connectionString")//判断是否存在connectionStrings这个节点
37 {
38 XmlNodeList node = element.ChildNodes;//得到当前节点下的子节点
39
40 if (node.Count > 0)//判断存在
41 {
42 DropDownList1.Items.Clear();
43
44 foreach (XmlElement el in node)
45 {
46 DropDownList1.Items.Add(el.Attributes["connectionString"].InnerXml);
47 }
48 }
49 }
50 }
51
52 }
53 /// <summary>
54 /// Web.Config 写的权限
55 /// </summary>
56 public void Write()
57 {
58 //获取根目录下的Web.Config文件的路径
59 string fileName=Server.MapPath("")+@"\Web.Config";
60
61
62 XmlDocument xmlDoc = new XmlDocument();//表示的是Xml文档
63
64 xmlDoc.Load(fileName);//加载xml文档
65
66 XmlNodeList xmlNode = xmlDoc.DocumentElement.ChildNodes;//获取该文档下的所有的节点
67
68 foreach (XmlElement element in xmlNode) //循环查找该节点下的元素
69 {
70 if (element.Name == "connectionString")//判断是否存在connectionStrings这个节点
71 {
72 XmlNodeList node = element.ChildNodes; //在子节点中循环查找
73
74 if (node.Count > 0)
75 {
76 foreach (XmlElement el in node)
77 {
78 if (el.Attributes["connectionString"].InnerXml.ToLower() == this.DropDownList1.SelectedItem.Value.ToLower())
79 {
80 //修改"name" 当前的Value 的值
81 el.Attributes["name"].Value = this.TextBox1.Text;
82 }
83 }
84 }
85
86 }
87 }
88 //保存所做的修改
89 xmlDoc.Save(fileName);
90
91 }
92 protected void Button1_Click(object sender, EventArgs e)
93 {
94 this.Write();
95
96 }
97}
98
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Xml; //引入的是命名空间
12public partial class Default3 : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!Page.IsPostBack)
17 {
18 this.read();
19 }
20 }
21 /// <summary>
22 /// 读取Web.Config中的节点
23 /// </summary>
24 public void read()
25 {
26 string fileName = Server.MapPath("") + @"\Web.Config";
27
28 XmlDocument xmlDoc = new XmlDocument();//表示的是Xml的文档
29
30 xmlDoc.Load(fileName);//加载Xml文档
31
32 XmlNodeList xmlNode = xmlDoc.DocumentElement.ChildNodes;//获取文档节点下的所有的子节点
33
34 foreach (XmlElement element in xmlNode)//在当前的节点中循环查找
35 {
36 if (element.Name == "connectionString")//判断是否存在connectionStrings这个节点
37 {
38 XmlNodeList node = element.ChildNodes;//得到当前节点下的子节点
39
40 if (node.Count > 0)//判断存在
41 {
42 DropDownList1.Items.Clear();
43
44 foreach (XmlElement el in node)
45 {
46 DropDownList1.Items.Add(el.Attributes["connectionString"].InnerXml);
47 }
48 }
49 }
50 }
51
52 }
53 /// <summary>
54 /// Web.Config 写的权限
55 /// </summary>
56 public void Write()
57 {
58 //获取根目录下的Web.Config文件的路径
59 string fileName=Server.MapPath("")+@"\Web.Config";
60
61
62 XmlDocument xmlDoc = new XmlDocument();//表示的是Xml文档
63
64 xmlDoc.Load(fileName);//加载xml文档
65
66 XmlNodeList xmlNode = xmlDoc.DocumentElement.ChildNodes;//获取该文档下的所有的节点
67
68 foreach (XmlElement element in xmlNode) //循环查找该节点下的元素
69 {
70 if (element.Name == "connectionString")//判断是否存在connectionStrings这个节点
71 {
72 XmlNodeList node = element.ChildNodes; //在子节点中循环查找
73
74 if (node.Count > 0)
75 {
76 foreach (XmlElement el in node)
77 {
78 if (el.Attributes["connectionString"].InnerXml.ToLower() == this.DropDownList1.SelectedItem.Value.ToLower())
79 {
80 //修改"name" 当前的Value 的值
81 el.Attributes["name"].Value = this.TextBox1.Text;
82 }
83 }
84 }
85
86 }
87 }
88 //保存所做的修改
89 xmlDoc.Save(fileName);
90
91 }
92 protected void Button1_Click(object sender, EventArgs e)
93 {
94 this.Write();
95
96 }
97}
98