应用程序启动时,加载配置文件,有两种方式解析XML,一种是XPATH,一种是用序列化,下面是用序列化:
http://www.yesky.com/20030218/1652674.shtml
1class 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
43public class SettingsHelper
44 {
45 SettingsItem 类
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 /// <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
43public class SettingsHelper
44 {
45 SettingsItem 类
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 }