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

一:创建序列化对象:

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文件

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

 

四: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

 

你可能感兴趣的:(Serialize)