惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Privacy International News Feed
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
博客园 - Franky
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
T
Tor Project blog
B
Blog RSS Feed
Recorded Future
Recorded Future
Scott Helme
Scott Helme
美团技术团队
V
V2EX
V
Visual Studio Blog
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
D
DataBreaches.Net
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
L
LangChain Blog
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
P
Privacy & Cybersecurity Law Blog

博客园 - Brendan

[转载]Manually configuring Microsoft Internet Information Services (IIS) IIS与TOMCAT协同工作---在IIS下运行JSP页面 AXIS部署错误解决方案集锦 使用System.Web.Mail发送Mail的错误解决方案 [翻译]你可以赚钱,但你不能赚时间 Windows 2003 Server配置IIS服务器(ASP, ASP.NET)全功略 Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive 用MS SQL Server事件探查器来跟踪数据库的操作 反反编译工具——Deploy.NET C中获取当前时间的函数 Buffered I/O 与 Non-Buffered I/O性能差异的实例体验 替换函数(Substitution Function) 主定理(Master Theorem) Dynamic Programming之Longest Increasing Subsequence (LIS)问题 ADO.NET数据库连接模块 ASP.NET控件事件丢失的探究 SQLDMO For C#(翻译) 关联(Association)设计中的扇形陷阱(Fan Traps)和断层陷阱(Chasm Traps) 简单并发控制
类的XML序列化(XML Serialization)
Brendan · 2006-07-12 · via 博客园 - Brendan

        最近做的一个ASP.NET项目中,需要在一个页面中维护一个类的数组,在每次页面刷新的使其前一次的状态保持不变。开始错误的使用了static,导致了致命的共享错误。后来突然想起C#类能够使用XML序列化出来,然后保存在XML里或者保存在页面的一个隐藏表单里(稍后再比较这两种方法的优劣)。下面来介绍这两个类序列化的应用。

保存于XML中的序列化C#类

先声明那个需要保存的类如下:

 1 [Serializable]
 2 public class HalfHour
 3 {
 4     public string ibtnHalfHourName;
 5     public int status;
 6     public bool isFirst;
 7     public int eventID;
 8     public bool isHead;
 9 
10         public HalfHour()
11     {
12         this.ibtnHalfHourName = "";
13         this.status = HalfHour.BLANK;
14         this.isFirst = false;
15         this.eventID = -1;
16         this.isHead = false;
17     }
18 
19 }

然后为这个类分别定义一个Serial和Deserial两个方法,分别完成序列化和反序列化这两件事(必要的namespace是System.Xml.Serialization和System.IO):

 1 public static void Serial(HalfHour[] halfHours, string path)
 2 {
 3     XmlSerializer xmlSerializer = new XmlSerializer(typeof(HalfHour[]));
 4     TextWriter writer = new StreamWriter(path);
 5     try
 6     {            
 7         xmlSerializer.Serialize(writer, halfHours);
 8     }
 9     finally
10     {
11         writer.Close();
12     }
13 }
14 
15 public static HalfHour[] Deserial(string path)
16 {
17     XmlSerializer xmlSerializer = new XmlSerializer(typeof(HalfHour[]));
18     FileStream fs = new FileStream(path, FileMode.Open);
19     HalfHour[] halfHours;
20     try
21     {
22         halfHours = (HalfHour[])xmlSerializer.Deserialize(fs);
23     }
24     finally
25     {
26         fs.Close();
27     }
28     return halfHours;
29 }

这两个函数的调用应该很容易理解:对于Serial,你只需提供XML的保存路径和你需要序列化的那个类数组。而Deserial则只需要提供读取的XML路径即可。

保存于隐藏表单中的序列化C#类

要序列化的类的声明和上面一致,这里不再重复。但你需要在你的Web页面中定义你的隐藏表单。
下面是Serial和Deserial函数的重载版本,这里要引入System.Text的namespace:

 1 public static string Serial(HalfHour[] halfHours)
 2 {
 3     XmlSerializer xmlSerializer = new XmlSerializer(typeof(HalfHour[]));
 4     MemoryStream memoryStream = new MemoryStream();
 5     xmlSerializer.Serialize(memoryStream, halfHours);
 6     return Encoding.UTF8.GetString(memoryStream.GetBuffer());
 7 }
 8 
 9 public static HalfHour[] Deserial(string serialedObject)
10 {
11     XmlSerializer xmlSerializer = new XmlSerializer(typeof(HalfHour[]));
12     MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(serialedObject));
13     return (HalfHour[])xmlSerializer.Deserialize(memoryStream);
14 }

两种方式的比较
       
        选择文件读写方式显然在服务器运行速度上会比较慢,而且要构思不产生冲突的文件名,同时还要考虑文件系统的读写权限问题。写入表单则没有这个问题,但是如果序列化的对象过大的话,会是整个页面文件变得很大,造成传输过程的缓慢。所以建议如果要序列化的对象较小,则选择后者。反之则选择前者。