我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

序列化

Posted on 2009-07-08 09:55 Hero 阅读(101) 评论(0)  编辑 收藏 引用 所属分类: C#积累
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 using System.IO;
 6 using System.Runtime.Serialization;
 7 using System.Runtime.Serialization.Formatters.Binary;
 8 
 9 namespace Serialize_test
10 {
11     class Program
12     {
13         static void Main( string[] args )
14         {
15             try
16             {
17                 //create products
18                 List<Product> products = new List<Product>();
19                 products.Add( new Product( 1"wang"100"good1" ) );
20                 products.Add( new Product( 2"zhao"200"good2" ) );
21                 products.Add( new Product( 4"ren"300"good3" ) );
22 
23                 Console.WriteLine( "Products to save :" );
24 
25                 foreach ( Product product in products )
26                 {
27                     Console.WriteLine( product );
28                 }
29                 Console.WriteLine();
30 
31                 //get serializer
32                 IFormatter serializer = new BinaryFormatter();
33 
34                 //serializer products
35                 FileStream saveFile = new FileStream( "Products.bin", FileMode.Create, FileAccess.Write );
36                 serializer.Serialize( saveFile, products );
37                 saveFile.Close();
38 
39                 //deserializer
40                 FileStream loadFile = new FileStream( "Products.bin", FileMode.Open, FileAccess.Read );
41                 List<Product> saveProducts = serializer.Deserialize( loadFile ) as List<Product>;
42                 loadFile.Close();
43 
44                 Console.WriteLine( "Products loaded :" );
45                 foreach ( Product product in saveProducts )
46                 {
47                     Console.WriteLine( product );
48                 }
49 
50             }
51             catch (SerializationException ex)
52             {
53                 Console.WriteLine( ex.Message );
54             }
55             catch ( IOException ex )
56             {
57                 Console.WriteLine( ex.ToString() );
58             }
59             
60         }
61     }
62 }
63 
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace Serialize_test
 6 {
 7     [Serializable]
 8     class Product
 9     {
10         public long ID;
11         public string Name;
12         public double Price;
13 
14         [NonSerialized]
15         string Notes;
16 
17         public Product( long id, string name, double price, string notes )
18         {
19             ID = id;
20             Name = name;
21             Price = price;
22             Notes = notes;
23         }
24 
25         public override string ToString()
26         {
27             return string.Format( "{0}:{1}(${2:F2}){3}", ID, Name, Price, Notes );
28         }
29     }
30 }
31 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理