C# XmlSerializer實現序列化淺析(轉載)

weixin_34219944發表於2009-12-10
  • C# XmlSerializer實現序列化淺析

 

  • C# XmlSerializer類是實現序列化的一個類,那麼關於C# XmlSerializer的學習我們要掌握怎麼樣的操作方法呢?那麼這裡向你詳細介紹具體的操作細節情況。

C# XmlSerializer是什麼呢?它是使用二進位制格式化程式進行序列化的一個類,那麼具體的通過C# XmlSerializer如何實現序列化操作呢?在序列化操作的過程中需要注意些什麼呢?

C# XmlSerializer的出處:

  1. using System.Xml.Serialization; 

C# XmlSerializer實現序列化:

  1. XmlSerializer xml = new XmlSerializer(typeof(Test));  
  2. FileStream fs = new FileStream(@"c:\t.xml",FileMode.Create);  
  3. xml.Serialize(fs, t);  
  4. fs.Close(); 

C# XmlSerializer實現反序列化

  1. FileStream fs = new FileStream(@"c:\t.xml", FileMode.Open);  
  2. XmlSerializer xml = new XmlSerializer(typeof(Test));  
  3. Test t = (Test)xml.Deserialize(fs); 

C# XmlSerializer類與主流的序列化類的幾個不同點是:

1、不需要Serializable屬性,Serializable和NonSerializable屬性將會被忽略,但是使用XmlIgnore屬性,和NonSerializable屬性類似。

2、該類不能安全地訪問私有變成員,所以學要將私有成員改為公共成員,或者提供合適的公共特性。

3、要求被序列化的類要有一個預設的構造器。

C# XmlSerializer的使用基本情況就向你介紹到這裡,希望對你瞭解和學習C# XmlSerializer類有所幫助,並且對序列化的操作有所認識。

 

轉載自:http://developer.51cto.com/art/200909/150786.htm

 

Object serialization is an important topic which is quite powerful if used correctly. Serialization allows programms to persist objects by storing then in files. In the case of this tutorial, into XML files. XML has become the standard for storage in the recent years and its good to see that with XML serialization built into the .NET framework, it will be very simple to build applications which can interop well with any other software, Microsoft or not.
This tutorial assumes a basic knowledge of programming. One thing that people may not be familiar with is the concept of attributes which is used heavily in this tutorial。

So there you have it. XML Serialization in C#. This is an incredibly useful way for application developers to persist objects by storing them in a file. It makes it really simple for any programmer to save configuration settings into XML files or application documents as well.

 

From:http://devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=236

相關文章