1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Xml; //引入的是命名空间
12
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98
