无痕客

落花无情,流水无痕……

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  • 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

posted on 2009-12-10 22:52  无痕客  阅读(6770)  评论(0编辑  收藏  举报