序列化 - 使用SoapFormatter

我们只要对前一个例子进行简单的修改就可以把我们的App改写成使用SoapFormatter。
1.对System..Runtime.Serialization.Formatters.Soap.dll的引用。(经过我的试验,如果在VS.NET下需要添加,用csc编译不需要)
2.将using语句中的一个using System.Runtime.Serialization.Formatters.Binary改为using System.Runtime.Serialization.Formatters.Soap
3.将所有BinaryFormatter替换为SoapFormatter
4.将生成数据文件扩展名改为.xml
最后的控制台输出与前面例子的一样。
附上修改后的程序:
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Collections;
None.gif
using  System.Runtime.Serialization;
None.gif
using  System.Runtime.Serialization.Formatters.Soap;
None.gif
None.gif[Serializable]
None.gif
public   class  Insect
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string name;
InBlock.gif    
InBlock.gif    [NonSerialized]
InBlock.gif    
private int id;
InBlock.gif    
InBlock.gif    
public Insect(string name, int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.name = name;
InBlock.gif        
this.id= id;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return String.Format("{0}:{1}", name, id);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
class  SerializeApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void  Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Insect i 
= new Insect("Meadow Brown"12);
InBlock.gif        Stream sw 
= File.Create("Insects.xml");
InBlock.gif        SoapFormatter bf 
= new SoapFormatter();
InBlock.gif        bf.Serialize(sw, i);
InBlock.gif        sw.Close();
InBlock.gif        ArrayList box 
= new ArrayList();
InBlock.gif        box.Add(
new Insect("Marsh Fritillary"34));
InBlock.gif        box.Add(
new Insect("Speckled Wood"56));
InBlock.gif        box.Add(
new Insect("Milkweed"78));
InBlock.gif        sw 
= File.Open("Insects.xml", FileMode.Append);
InBlock.gif        bf.Serialize(sw, box);
InBlock.gif        sw.Close();
InBlock.gif        
InBlock.gif        Stream sr 
= File.OpenRead("Insects.xml");
InBlock.gif        Insect j 
= (Insect)bf.Deserialize(sr);
InBlock.gif        Console.WriteLine(j);
InBlock.gif        
InBlock.gif        ArrayList bag 
= (ArrayList)bf.Deserialize(sr);
InBlock.gif        sr.Close();
InBlock.gif        
foreach(Insect k in bag)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(k);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

附上打开VS.NET中打开Insect.XML的截图:
SoapFormatter.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#序列化和反序列化是将对象转换为容易传输的格式,并重新解析构造被序列化的对象的过程。当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要将对象转换为字节序列,才能在网络上传送;接收方则需要将字节序列再转换为对象。 为什么要序列化和反序列化呢?我们经常需要将对象的字段值保存在磁盘中,并在以后检索这些数据。尽管不使用序列化也能完成这项工作,但这样的方法通常繁琐而且容易出错。当需要跟踪对象的层次结构时,使用序列化会变得越来越复杂。对于包含大量对象的大型业务应用程序来说,程序员不得不为每个对象编写代码来保存和还原字段和属性。序列化提供了一种轻松实现这个目标的快捷方法。.NET框架通过使用反射提供自动的序列化机制,它会将对象序列化后的类名称、程序集以及类实例的所有数据成员写入存储媒体中。 .NET框架提供了多种序列化方式,包括BinaryFormatterSoapFormatter和XmlSerializer。BinaryFormatter提供了一个简单的二进制数据流以及某些附加的类型信息;SoapFormatter将数据流格式化为XML存储;XmlSerializer也是将数据流格式化为XML存储,但相对于SoapFormatter来说更简化。可以使用[Serializable]属性将一个类标记为可序列化的。如果某个类的成员不想被序列化,可以使用[NonSerialized]属性来标记,或者使用[XmlIgnore]属性来标记。 总结一下,C#序列化和反序列化是将对象转换为容易传输的格式,并重新解析构造被序列化的对象的过程。它在两个进程进行远程通信、保存和检索对象字段值等场景下发挥着重要作用。.NET框架提供了多种序列化方式,并通过使用属性来控制哪些成员需要被序列化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值