01-Remoting之序列化(BinaryFormatter,SoapFormatter,XmlSerializer)

本文介绍了一个使用 C# 实现的序列化示例,包括创建序列化对象、生成对象实例、采用 BinaryFormatter、SoapFormatter 和 XmlSerializer 进行序列化的全过程,并展示了生成的 XML 文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:创建序列化对象:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  SerializationDemo
{
    [Serializable]
    
public   class  SumOf
    {
        
public  SumOf()
        {
        }

        
public  DecimalList Members  =   new  DecimalList();

        
public   decimal  Sum,Avg;

        
public   void  Calculate()
        {
            
this .Sum  =   0 ;
            
foreach  ( decimal  m  in  Members)
            {
                Sum 
+=  m;
            }
            
this .Avg  =  Sum  /  Members.Count;

        }

    }
    [Serializable]
    
public   class  DecimalList : List < decimal >
    {
    }

}  


二: 生成对象:

         private  SumOf BuildSumObj()
        {
            SumOf sObj 
=   new  SumOf();
            
for  ( int  i  =   0 ; i  <   this .numericUpDown1.Value; i ++ )
            {
                sObj.Members.Add(i);
            }
            sObj.Calculate();
            
return  sObj;
        }

        
private   void  button3_Click( object  sender, EventArgs e)
        {
            sObj 
=   this .BuildSumObj();
            
this .toolStripStatusLabel1.Text  =
                
string .Format( " 数量:{0},合计:{1},平均值:{2} " , sObj.Members.Count, sObj.Sum, sObj.Avg);
        } 


 三:BinaryFormatter

   private   void  button1_Click( object  sender, EventArgs e)
        {
            
//  create a file stream to write the file
            FileStream fileStream  =   new  FileStream( " DoSum.bin " , FileMode.Create);
            
//  use the CLR binary formatter
            BinaryFormatter binaryFormatter  =   new  BinaryFormatter();
            
//  serialize to disk
            binaryFormatter.Serialize(fileStream, sObj);
            fileStream.Close();
  } 


结果:生成DoSum.bin文件

 

 

四:SoapFormatter

         private   void  button2_Click( object  sender, EventArgs e)
        {
            
//  create a file stream to write the file
            FileStream fileStream  =   new  FileStream( " DoSum_Soap.xml " , FileMode.Create);
            
//  use the CLR binary formatter
            SoapFormatter formatter  =   new  SoapFormatter();
            
//  serialize to disk
            formatter.Serialize(fileStream, sObj);
            fileStream.Close();
        } 


结果:生成DoSum_Soap.xml

< 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:SumOf id = " ref-1 "  xmlns:a1 = " http://schemas.microsoft.com/clr/nsassem/SerializationDemo/SerializationDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull " >
< Members href = " #ref-3 " />
< Sum > 45 </ Sum >
< Avg > 4.5 </ Avg >
</ a1:SumOf >
< a1:DecimalList id = " ref-3 "  xmlns:a1 = " http://schemas.microsoft.com/clr/nsassem/SerializationDemo/SerializationDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull " >
< List_x0060_1_x002B__items href = " #ref-4 " />
< List_x0060_1_x002B__size > 10 </ List_x0060_1_x002B__size >
< List_x0060_1_x002B__version > 10 </ List_x0060_1_x002B__version >
</ a1:DecimalList >
< SOAP - ENC:Array id = " ref-4 "  SOAP - ENC:arrayType = " xsd:decimal[16] " >
< item > 0 </ item >
< item > 1 </ item >
< item > 2 </ item >
< item > 3 </ item >
< item > 4 </ item >
< item > 5 </ item >
< item > 6 </ item >
< item > 7 </ item >
< item > 8 </ item >
< item > 9 </ item >
< item > 0 </ item >
< item > 0 </ item >
< item > 0 </ item >
< item > 0 </ item >
< item > 0 </ item >
< item > 0 </ item >
</ SOAP - ENC:Array >
</ SOAP - ENV:Body >

</SOAP-ENV:Envelope>  


五:XmlSerializer

  private   void  button4_Click( object  sender, EventArgs e)
        {
            
//  create a file stream to write the file
            FileStream fileStream  =   new  FileStream( " DoSum.xml " , FileMode.Create);
            
//  use the CLR binary formatter
            System.Xml.Serialization.XmlSerializer
                formatter 
=   new  XmlSerializer( typeof (SumOf));
            
//  serialize to disk
            formatter.Serialize(fileStream, sObj);
            fileStream.Close();

        }  

结果:

<? xml version = " 1.0 " ?>
< SumOf xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "  xmlns:xsd = " http://www.w3.org/2001/XMLSchema " >
  
< Members >
    
< decimal > 0 </ decimal >
    
< decimal > 1 </ decimal >
    
< decimal > 2 </ decimal >
    
< decimal > 3 </ decimal >
    
< decimal > 4 </ decimal >
    
< decimal > 5 </ decimal >
    
< decimal > 6 </ decimal >
    
< decimal > 7 </ decimal >
    
< decimal > 8 </ decimal >
    
< decimal > 9 </ decimal >
  
</ Members >
  
< Sum > 45 </ Sum >
  
< Avg > 4.5 </ Avg >

</SumOf>  


 

 Demo

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值