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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - 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 }

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