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

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

 找回密碼
 注冊(開放注冊)
查看: 10037|回復(fù): 0
打印 上一主題 下一主題

[應(yīng)用技術(shù)] Xml 串行話對象與反串行實例

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2007-11-10 14:39:22 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
一個簡單的例子,但是很實用。將對象直接串行話為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
您需要登錄后才可以回帖 登錄 | 注冊(開放注冊)

本版積分規(guī)則

手機訪問本頁請
掃描左邊二維碼
本網(wǎng)站聲明
本網(wǎng)站所有內(nèi)容為網(wǎng)友上傳,若存在版權(quán)問題或是相關(guān)責任請聯(lián)系站長!
站長電話:0898-66661599    站長聯(lián)系QQ:12726567   
站長微信:hainanok
請掃描右邊二維碼
www.myujob.com

小黑屋|手機版|Archiver|電子科大論壇-非清水河畔 ( 瓊ICP備10001196號-2 )

GMT+8, 2025-11-6 22:24 , Processed in 0.081394 second(s), 14 queries .

Powered by 校園招聘信息

© 2001-2020 電子科大論壇-非清水河畔校園招聘

快速回復(fù) 返回頂部 返回列表