类:Config.cs
使用:
取:
1
using
System;
2
using
System.Xml;
3
using
System.Reflection;
4
using
System.Configuration;
5
using
System.Windows.Forms;
6
7
namespace
XmlConfig
8
{
9
public class Config : System.Configuration.AppSettingsReader
10
{
11
private XmlNode node;
12
private string _cfgFile;
13
14
public string cfgFile
15
{
16
get
{ return _cfgFile; }
17
set
{ _cfgFile= Application.StartupPath + "//" + value; }
18
}
19
20
public string GetValue (string key)
21
{
22
return Convert.ToString(GetValue(key, typeof(string)));
23
}
24
25
public new object GetValue (string key, System.Type sType)
26
{
27
XmlDocument doc = new XmlDocument();
28
object ro = String.Empty;
29
loadDoc(doc);
30
string sNode = key.Substring(0, key.LastIndexOf("//"));
31
// retrieve the selected node
32
try
33
{
34
node = doc.SelectSingleNode(sNode);
35
if( node != null )
36
{
37
// Xpath selects element that contains the key
38
XmlElement targetElem= (XmlElement)node.SelectSingleNode(key) ;
39
if (targetElem!=null)
40
{
41
ro = targetElem.GetAttribute("value");
42
}
43
}
44
if (sType == typeof(string))
45
return Convert.ToString(ro);
46
else
47
if (sType == typeof(bool))
48
{
49
if (ro.Equals("True") || ro.Equals("False"))
50
return Convert.ToBoolean(ro);
51
else
52
return false;
53
}
54
else
55
if (sType == typeof(int))
56
return Convert.ToInt32(ro);
57
else
58
if (sType == typeof(double))
59
return Convert.ToDouble(ro);
60
else
61
if (sType == typeof(DateTime))
62
return Convert.ToDateTime(ro);
63
else
64
return Convert.ToString(ro);
65
}
66
catch
67
{
68
return String.Empty;
69
}
70
}
71
72
public bool SetValue (string key, string val)
73
{
74
XmlDocument doc = new XmlDocument();
75
loadDoc(doc);
76
try
77
{
78
// retrieve the target node
79
string sNode = key.Substring(0, key.LastIndexOf("//"));
80
node = doc.SelectSingleNode(sNode);
81
if( node == null )
82
return false;
83
// Set element that contains the key
84
XmlElement targetElem= (XmlElement) node.SelectSingleNode(key);
85
if (targetElem!=null)
86
{
87
// set new value
88
targetElem.SetAttribute("value", val);
89
}
90
// create new element with key/value pair and add it
91
else
92
{
93
94
sNode = key.Substring(key.LastIndexOf("//")+2);
95
96
XmlElement entry = doc.CreateElement(sNode.Substring(0, sNode.IndexOf("[@")).Trim());
97
sNode = sNode.Substring(sNode.IndexOf("'")+1);
98
99
entry.SetAttribute("key", sNode.Substring(0, sNode.IndexOf("'")) );
100
101
entry.SetAttribute("value", val);
102
node.AppendChild(entry);
103
}
104
saveDoc(doc, this._cfgFile);
105
return true;
106
}
107
catch
108
{
109
return false;
110
}
111
}
112
113
private void saveDoc (XmlDocument doc, string docPath)
114
{
115
// save document
116
// choose to ignore if web.config since it may cause server sessions interruptions
117
if( this._cfgFile.Equals("web.config") )
118
return;
119
else
120
try
121
{
122
XmlTextWriter writer = new XmlTextWriter( docPath , null );
123
writer.Formatting = Formatting.Indented;
124
doc.WriteTo( writer );
125
writer.Flush();
126
writer.Close();
127
return;
128
}
129
catch
130
{}
131
}
132
133
public bool removeElement (string key)
134
{
135
XmlDocument doc = new XmlDocument();
136
loadDoc(doc);
137
try
138
{
139
string sNode = key.Substring(0, key.LastIndexOf("//"));
140
// retrieve the appSettings node
141
node = doc.SelectSingleNode(sNode);
142
if( node == null )
143
return false;
144
// XPath select setting "add" element that contains this key to remove
145
XmlNode nd = node.SelectSingleNode(key);
146
node.RemoveChild(nd);
147
saveDoc(doc, this._cfgFile);
148
return true;
149
}
150
catch(Exception ex)
151
{
152
Console.WriteLine(ex.Message);
153
return false;
154
}
155
}
156
157
private void loadDoc ( XmlDocument doc )
158
{
159
// check for type of config file being requested
160
/**//*
161
if( this._cfgFile.Equals("app.config"))
162
{
163
// use default app.config
164
this._cfgFile = ((Assembly.GetEntryAssembly()).GetName()).Name+".exe.config";
165
}
166
else
167
if( this._cfgFile.Equals("web.config"))
168
{
169
// use server web.config
170
this._cfgFile = System.Web.HttpContext.Current.Server.MapPath("web.config");
171
}
172
*/
173
// load the document
174
175
doc.Load(this._cfgFile );
176
}
177
178
}
179
}
180

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

99

100

101

102

103

104

105

106

107

108



109

110

111

112

113

114



115

116

117

118

119

120

121



122

123

124

125

126

127

128

129

130



131

132

133

134



135

136

137

138



139

140

141

142

143

144

145

146

147

148

149

150

151



152

153

154

155

156

157

158



159

160


161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

使用:
取:
Config config = new Config();
config.cfgFile = "app.config";
txtCountry.Text =
config.GetValue("//appSettings//add[@key='CountryLoc']");
写:
config.SetValue("//appSettings//add[@key='"
+ txtKey.Text + "']", txtValue.Text);
删除:
config.removeElement("//appSettings//add[@key='" +
txtKey2.Text + "']");