C# 串行化和反串行化存储数据示例

       可以将对象和结构体转化为某种格式,从而适合网络传输和数据存储(串行化主要完成两个任务:实现WEB服务和将集合对象存储在介质之中)。在.NET中,这个转换过程就成为串行化(Serialization),逆串行化(Deserialization)是串行化的逆过程,可以将串行化流转换为原来的对象或者结构体。

      .NET主要支持三种主要的串行化:

     1,二进制串行化(Binary),使用BinaryFormatter类,将类型串行化为二进制流。

     2,SOAP串行化。使用SoapFormatter类,将类型串行化为遵循简单对象访问协议标准的XML格式。

     3,XML串行化,使用XMLSerializer类,将类型串行化为基本的XML,WEB服务就使用这样的串行化。

           对类进行XML串行化需要注意以下几点:

          (1)该类必须有公共的无参构造函数

          (2)只能串行化公共的属性和字段

          (3)不能串行化只读的属性

          (4)若要串行化定制集合类中的对象,则该类必须从System.Collection.CollectionBase类派生,并包含索引器。

          (5)要串行化多个对象,通常最简单的方法是将他们存放在强类型的数组或链表等容器中。

     二进制串行化示例:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Runtime.Serialization;
  5 using System.Runtime.Serialization.Formatters.Binary;
  6 using System.IO;
  7 using System.Collections;
  8 
  9 namespace SerializeDemo
 10 {
 11     /// <summary>
 12     /// 用户信息结构体
 13     /// </summary>
 14     [Serializable]
 15     struct UserInfo
 16     {
 17         public UserInfo(string id,string name,string password)
 18         {
 19             this.id = id;
 20             this.name = name;
 21             this.password = password;
 22         }
 23         string id;
 24         public string Id
 25         {
 26             get { return id; }
 27         }
 28         string name;
 29         public string Name
 30         {
 31             get { return name; }
 32         }
 33         string password;
 34         public string Password
 35         {
 36             get { return password; }
 37         }
 38     }
 39     /// <summary>
 40     /// 商品信息结构体
 41     /// </summary>
 42     [Serializable]
 43     struct Goods
 44     {
 45         public Goods(string id,string name,double price)
 46         {
 47             this.id = id;
 48             this.name = name;
 49             this.price = price;
 50         }
 51         string id;
 52         public string Id
 53         {
 54             get { return id; }
 55         }
 56         string name;
 57         public string Name
 58         {
 59             get { return name; }
 60         }
 61         double price;
 62         public double Price
 63         {
 64             get { return price; }
 65         }
 66 
 67     }
 68 
 69     class Program
 70     {
 71         static void Main(string[] args)
 72         {
 73             ArrayList arr = new ArrayList();
 74             for (int i = 0; i < 1000; i++)
 75             {
 76                 UserInfo info = new UserInfo("Id" + i.ToString(), "Name" + i.ToString(), "PassWord" + i.ToString());
 77                 arr.Add(info);
 78             }
 79             for (int i = 0; i < 1000; i++)
 80             {
 81                 Goods info = new Goods("Id" + i.ToString(), "Good" + i.ToString(), i);
 82                 arr.Add(info);
 83             }
 84 
 85             FileStream fs = new FileStream(@"C:\Users\Jessiane\Desktop\data.dat", FileMode.Create);
 86             BinaryFormatter bf = new BinaryFormatter();
 87             bf.Serialize(fs, arr);
 88             fs.Close();
 89             arr.Clear();
 90             fs = new FileStream(@"C:\Users\Jessiane\Desktop\data.dat", FileMode.Open);
 91             arr = (ArrayList)bf.Deserialize(fs);
 92             while (true)
 93             {
 94                 Console.WriteLine("输入检索名");
 95                 string name = Console.ReadLine();
 96                 foreach (object o in arr)
 97                 {
 98                     if (o is Goods)
 99                     {
100                         Goods info = (Goods)o;
101                         if (info.Name == name)
102                         {
103                             Console.WriteLine(info.Id.ToString());
104                         }
105                     }
106                 }
107             }
108         }
109     }
110 }

 

转载于:https://www.cnblogs.com/runonroad/p/4013829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值