应用程序启动时,加载配置文件,有两种方式解析XML,一种是XPATH,一种是用序列化,下面是用序列化:
http://www.yesky.com/20030218/1652674.shtml
1
class SerializerHelper
2
{
3
/**//// <summary>
4
/// 序列化:写文件,将对象序列化成文件格式的文本,这里是XML
5
/// </summary>
6
/// <param name="obj">要序列化的对象</param>
7
/// <param name="filePath">文件路径</param>
8
public void SerializeObject(object obj,string filePath)
9
{
10
TextWriter writer = new StreamWriter(filePath);
11
XmlSerializer xmlSer = new XmlSerializer(obj.GetType());
12
xmlSer.Serialize(writer, obj);
13
writer.Close();
14
}
15
16
/**//// <summary>
17
/// 反序列化:读文件,将文件格式的文本(这里是XML)反序列化成对象
18
/// </summary>
19
/// <param name="t">对象类型</param>
20
/// <param name="filePath">文件路径</param>
21
/// <returns></returns>
22
public object DeserializeObject(Type t, string filePath)
23
{
24
TextReader reader = new StreamReader(filePath);
25
XmlSerializer xmlSer = new XmlSerializer(t);
26
return xmlSer.Deserialize(reader);
27
}
28
29
/**//// <summary>
30
/// 反序列化:读文件,将文件格式的文本(这里是XML)反序列化成对象
31
/// </summary>
32
/// <typeparam name="TObject">范型,指所有类型</typeparam>
33
/// <param name="filePath">文件路径</param>
34
/// <returns></returns>
35
public TObject DeserializeObject<TObject>(string filePath)
36
{
37
TextReader reader = new StreamReader(filePath);
38
XmlSerializer xmlSer = new XmlSerializer(typeof(TObject));
39
return (TObject)xmlSer.Deserialize(reader);
40
}
41
}
42
43
public class SettingsHelper
44
{
45
SettingsItem 类#region SettingsItem 类
46
[Serializable()]
47
public class SettingsItem
48
{
49
Constructor#region Constructor
50
/**//// <summary>
51
///
52
/// </summary>
53
public SettingsItem()
54
{
55
}
56
#endregion
57
58
Fields#region Fields
59
string m_PluginXmlFileName;
60
string m_WebServiceConfiguration;
61
string m_WorkDir;
62
string m_TmpDir;
63
string m_DecimalFormatString;
64
string m_DepartmentTitle;
65
string m_ApplicationTitle;
66
bool m_IsSingleApplication;
67
string m_BudgetYear;
68
string m_LastUserId;
69
string m_SkinName;
70
bool m_AutoUpgrade;
71
bool m_Setting;
72
int m_Style;
73
#endregion
74
75
Properties#region Properties
76
/**//// <summary>
77
/// PluginConfig 配置文件
78
/// </summary>
79
public string PluginXmlFileName
80
{
81
get
{ return m_PluginXmlFileName; }
82
set
{ m_PluginXmlFileName = value; }
83
}
84
85
/**//// <summary>
86
/// WebService 客户端配置文件
87
/// </summary>
88
public string WebServiceConfiguration
89
{
90
get
{ return m_WebServiceConfiguration; }
91
set
{ m_WebServiceConfiguration = value; }
92
}
93
94
/**//// <summary>
95
/// WorkDir 名称
96
/// </summary>
97
public string WorkDir
98
{
99
get
{ return m_WorkDir; }
100
set
{ m_WorkDir = value; }
101
}
102
103
/**//// <summary>
104
/// TmpDir 名称
105
/// </summary>
106
public string TmpDir
107
{
108
get
{ return m_TmpDir; }
109
set
{ m_TmpDir = value; }
110
}
111
112
/**//// <summary>
113
/// Decimal 数据的格式
114
/// </summary>
115
public string DecimalFormatString
116
{
117
get
{ return m_DecimalFormatString; }
118
set
{ m_DecimalFormatString = value; }
119
}
120
121
/**//// <summary>
122
/// 基础资料树中查看详细信息查看名称
123
/// </summary>
124
public string DepartmentTitle
125
{
126
get
{ return m_DepartmentTitle; }
127
set
{ m_DepartmentTitle = value; }
128
}
129
130
/**//// <summary>
131
/// 应用程序的标题
132
/// </summary>
133
public string ApplicationTitle
134
{
135
get
{ return m_ApplicationTitle; }
136
set
{ m_ApplicationTitle = value; }
137
}
138
139
/**//// <summary>
140
/// 是否只运行单个应用程序
141
/// </summary>
142
public bool IsSingleApplication
143
{
144
get
{ return m_IsSingleApplication; }
145
set
{ m_IsSingleApplication = value; }
146
}
147
148
/**//// <summary>
149
/// 基础资料树中查看详细信息查看名称
150
/// </summary>
151
public string BudgetYear
152
{
153
get
{ return m_BudgetYear; }
154
set
{ m_BudgetYear = value; }
155
}
156
157
/**//// <summary>
158
/// 当前登录的用户ID
159
/// </summary>
160
public string LastUserId
161
{
162
get
{ return m_LastUserId; }
163
set
{ m_LastUserId = value; }
164
}
165
166
/**//// <summary>
167
/// 当Style=4时的样式名称
168
/// 例:Stardust代表梦幻,Money Twins代表货币,The Asphalt World代表清爽,
169
/// Caramel代表怀旧,Liquid Sky代表湖蓝,Coffee代表咖啡,Glass Oceans代表玻璃
170
/// </summary>
171
public string SkinName
172
{
173
get
{ return m_SkinName; }
174
set
{ m_SkinName = value; }
175
}
176
177
/**//// <summary>
178
/// 应用程序启动时,是否自动更新
179
/// </summary>
180
public bool AutoUpgrade
181
{
182
get
{ return m_AutoUpgrade; }
183
set
{ m_AutoUpgrade = value; }
184
}
185
186
/**//// <summary>
187
///
188
/// </summary>
189
public bool Setting
190
{
191
get
{ return m_Setting; }
192
set
{ m_Setting = value; }
193
}
194
195
/**//// <summary>
196
/// 样式,例:0代表扁平,1代表超扁平,2代表经典,3代表Office2003,4代表皮肤
197
/// </summary>
198
public int Style
199
{
200
get
{ return m_Style; }
201
set
{ m_Style = value; }
202
}
203
#endregion
204
}
205
#endregion
206
207
void Deserialize()
208
{
209
SerializerHelper ser = new SerializerHelper();
210
//SettingsItem set = ser.DeserializeObject<SettingsItem>(@"ApplicationConfig\TestConfig.xml");
211
SettingsItem set = (SettingsItem)ser.DeserializeObject(typeof(SettingsItem), @"ApplicationConfig\TestConfig.xml");
212
213
Console.WriteLine(set.ApplicationTitle);
214
Console.WriteLine(set.DepartmentTitle);
215
Console.WriteLine(set.IsSingleApplication);
216
}
217
218
void Serialize()
219
{
220
SerializerHelper ser = new SerializerHelper();
221
SettingsItem set = new SettingsItem();
222
set.ApplicationTitle = "dengqian";
223
set.AutoUpgrade = true;
224
set.BudgetYear = "2008";
225
set.DecimalFormatString = "######0000";
226
set.DepartmentTitle = "预算年份";
227
ser.SerializeObject(set, @"ApplicationConfig\TestConfig.xml");
228
229
Console.WriteLine(set.ApplicationTitle);
230
Console.WriteLine(set.DepartmentTitle);
231
Console.WriteLine(set.IsSingleApplication);
232
}
233
234
public static void main()
235
{
236
SettingsHelper helper = new SettingsHelper();
237
helper.Serialize();
238
Console.WriteLine("-----------------------");
239
helper.Deserialize();
240
}
241
}

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

181



182



183



184

185

186


187

188

189

190



191



192



193

194

195


196

197

198

199



200



201



202

203

204

205

206

207

208



209

210

211

212

213

214

215

216

217

218

219



220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235



236

237

238

239

240

241
