影音先锋男人资源av站_狠狠色综合激情丁香五月_爱爱爱爱看视频_在线播放免费人成视频在线观看_少妇人妻综合久久中文字幕_国产午夜无码精品免费看_久久久久久夜精品精品免费啦_男人女人午夜视频免费_日本xxxx裸体xxxx_丰满人妻熟妇乱又仑精品

電子科大論壇-非清水河畔

標題: Xml 串行話對象與反串行實例 [打印本頁]

作者: songlai    時間: 2007-11-10 14:39
標題: Xml 串行話對象與反串行實例
一個簡單的例子,但是很實用。將對象直接串行話為xml文件,并可以反串行話為對象。
方法:


1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Xml;
6using System.Xml.Serialization;
7using System.Collections.ObjectModel;
8
9namespace XmlBuilder
10{
11 /**////
12 ///
13 ///
14 public static class Localization
15 {
16 公有方法#region 公有方法
17 /**////
18 /// 序列化對象
19 ///
20 /// 文件信息名稱
21 /// 待序列化對象
22 public static void SerializeObject(string fileName, object obj)
23 {
24 Stream writer = null;
25 try
26 {
27 Type type = obj.GetType();
28 XmlSerializer serializer = new XmlSerializer(type);
29
30 writer = new FileStream(fileName, FileMode.Create);
31 serializer.Serialize(writer, obj);
32 }
33 catch(Exception ex)
34 {
35 throw ex;
36 }
37 finally
38 {
39 if(writer != null)
40 writer.Close();
41 }
42 }
43
44 /**////
45 /// 反序列化對象
46 ///
47 /// 文件信息名稱
48 /// 反序列化對象類型
49 /// 返回實例化后對象
50 public static object DeSerializeObject(string fileName, Type type)
51 {
52 if (!File.Exists(fileName))
53 return null;
54
55 FileStream fs = null;
56 try
57 {
58 fs = new FileStream(fileName, FileMode.Open);
59
60 XmlSerializer xSer = new XmlSerializer(type);
61 XmlReader reader = new XmlTextReader(fs);
62 return xSer.Deserialize(reader);
63 }
64 catch (Exception ex)
65 {
66 throw ex;
67 }
68 finally
69 {
70 if (fs != null)
71 {
72 fs.Close();
73 }
74 }
75 }
76 #endregion
77 }
78}
79





歡迎光臨 電子科大論壇-非清水河畔 (http://www.hallmarkedu.com/) Powered by Discuz! X3.4