• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个具有上进心的码农
因为一篇文章中有很多是从很多篇文章中摘取的,请恕我没有一一说明摘取出处,如果没有说明,则该文章默认是摘取,如有侵犯您的权益,请与我联系,将会马上删除。
博客园    首页    新随笔    联系   管理    订阅  订阅

简单的序列化

序列化: 把类存在文件中。
反序列化:把文件中的信息读取到类中。
这也是我很肤浅的理解。 主要是实用。
首先谈一下很重要的一张图。

XML SOAP 二进制
序列化器类 XmlSerializer SoapFormatter BinaryFormatter
SerializableAttribute  标记 不需要 需要
ISerializable 接口 不需要实现,实现了也不起作用。 可以不实现,但实现了就起作用。
无参构造函数 必须有,系统提供的缺省无参构造函数也算。 不需要,因为反序列化时不调用构造函数。
被序列化的数据成员 公共属性和字段 所有
产生文件大小 大 大 小

xml的序列化 将类存在XML文件上面去。


    public static BaseConfigInfo GetRealBaseConfig()
    {
        BaseConfigInfo newBaseConfig = null;
        string filename = null;
        HttpContext context = HttpContext.Current;
        if (context != null)
            filename = context.Server.MapPath("~/LabelPrint/test1.config");
        else
            filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/HLT.config");

        try
        {
            newBaseConfig = (BaseConfigInfo)ConfigFileManager.DeserializeInfo(filename, typeof(BaseConfigInfo));
        }
        catch
        {
            newBaseConfig = null;
        }


        if (newBaseConfig == null)
        {
            throw new Exception("发生错误: 网站Config目录下没有正确的HLT.config文件");
        }
        return newBaseConfig;
}


写一下类的形成

 [Serializable]
    public class BaseConfigInfo
    {
        public BaseConfigInfo() { }
        public EmailConfigInfo EmailConfigInfo
        {
            get;
            set;
        }
        public SystemConfigInfo SystemConfigInfo
        { get; set; }
     
    }

二进制的序列化

 [Serializable]
    
public class SerializableClass
    
{
        
string name = string.Empty;
        
int age = 0;
        
string grade = string.Empty;
        
public SerializableClass()
        
{

        }


        
public string Name
        
{
            
get
            
{
                
return name;
            }

            
set
            
{
                name 
= value;
            }

        }


        
public int Age
        
{
            
get
            
{
                
return age;
            }

            
set
            
{
                age 
= value;
            }

        }


        
public string Grade
        
{
            
get
            
{
                
return grade;
            }

            
set
            
{
                grade 
= value;
            }

        }

    }


实现序列化和反序列化
需要引用的命名空间
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


  public void SerialzableCless()
        
{
            
//将对象序列化
            IFormatter format = new BinaryFormatter();
            Stream stream 
= new FileStream(@"c:\fastyou.bin",FileMode.Create,FileAccess.Write,FileShare.None);
            SerializableClass sc 
= new SerializableClass();
            sc.Age 
= 26;
            sc.Name 
= "fastyou";
            sc.Grade 
= "Advance";
            format.Serialize(stream, sc);
            stream.Close();
            
//将文件流反序列化
            Stream dstream = new FileStream(@"c:\fastyou.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            SerializableClass scc 
= (SerializableClass)format.Deserialize(dstream);
            dstream.Close();
        }


对同一个类进行Soap 序列化
需要的命名空间
using System.Runtime.Serialization.Formatters.Soap;
还需要add System.Runtime.Serialization.Formatters.Soap.dll
public void SoapSerialzable()
        
{
            
//将对象序列化
            SoapFormatter format = new SoapFormatter();
            Stream stream 
= new FileStream(@"c:\fastyou.soap", FileMode.Create, FileAccess.Write, FileShare.None);
            SerializableClass sc 
= new SerializableClass();
            sc.Age 
= 26;
            sc.Name 
= "fastyou";
            sc.Grade 
= "Advance";
            format.Serialize(stream, sc);
            stream.Close();
            
//将文件流反序列化
            Stream dstream = new FileStream(@"c:\fastyou.soap", FileMode.Open, FileAccess.Read, FileShare.Read);
            SerializableClass scc 
= (SerializableClass)format.Deserialize(dstream);
            dstream.Close();

        }



<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:SerializableClass id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Test/Test%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<name id="ref-3">fastyou</name>
<age>26</age>
<grade id="ref-4">Advance</grade>
</a1:SerializableClass>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

posted @ 2008-06-18 17:39  不若相忘于江湖  阅读(149)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3