二进制、SOAP的序列化及反序列化

二进制的序列化,反序列化。
需要序列化的类:

None.gif
None.gif    [Serializable]
None.gif    
public   class  SerializableClass
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
string name = string.Empty;
InBlock.gif        
int age = 0;
InBlock.gif        
string grade = string.Empty;
InBlock.gif        
public SerializableClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return name;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                name 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return age;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                age 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Grade
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return grade;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                grade 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

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

None.gif public   void  SoapSerialzable()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//将对象序列化
InBlock.gif
            SoapFormatter format = new SoapFormatter();
InBlock.gif            Stream stream 
= new FileStream(@"c:\fastyou.soap", FileMode.Create, FileAccess.Write, FileShare.None);
InBlock.gif            SerializableClass sc 
= new SerializableClass();
InBlock.gif            sc.Age 
= 26;
InBlock.gif            sc.Name 
= "fastyou";
InBlock.gif            sc.Grade 
= "Advance";
InBlock.gif            format.Serialize(stream, sc);
InBlock.gif            stream.Close();
InBlock.gif            
//将文件流反序列化
InBlock.gif
            Stream dstream = new FileStream(@"c:\fastyou.soap", FileMode.Open, FileAccess.Read, FileShare.Read);
InBlock.gif            SerializableClass scc 
= (SerializableClass)format.Deserialize(dstream);
InBlock.gif            dstream.Close();
InBlock.gif
ExpandedBlockEnd.gif        }
下面是序列化后的文件
None.gif < 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/" >
None.gif
< SOAP-ENV:Body >
None.gif
< 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" >
None.gif
< name  id ="ref-3" > fastyou </ name >
None.gif
< age > 26 </ age >
None.gif
< grade  id ="ref-4" > Advance </ grade >
None.gif
</ a1:SerializableClass >
None.gif
</ SOAP-ENV:Body >
None.gif
</ SOAP-ENV:Envelope >

转载于:https://www.cnblogs.com/laihua/archive/2007/07/30/836880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值