Advertisement
NAK

Serialization BinaryFormatter (C#)

NAK
Nov 30th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             while (true)
  13.             {
  14.                 Console.WriteLine("s=serialize, r=read:");
  15.                 switch (Console.ReadLine())
  16.                 {
  17.                     case "s": // save the collection of objects
  18.  
  19.                         List<Employee> EmployeeList1 = new List<Employee>();
  20.  
  21.                         // create some objects adding them to a List
  22.                         EmployeeList1.Add(new Employee(1, "Elenor", "Scheller"));
  23.                         EmployeeList1.Add(new Employee(2, "Kaila", "Wilburn"));
  24.                         EmployeeList1.Add(new Employee(3, "Shea", "Wallen"));
  25.                         EmployeeList1.Add(new Employee(4, "Elliot", "Shaffer"));
  26.                         EmployeeList1.Add(new Employee(5, "Dominica", "Charney"));
  27.  
  28.                         try // try to serialize the collection to a file
  29.                         {
  30.                             using (Stream stream = File.Open("data.bin", FileMode.Create))
  31.                             {
  32.                                 // create BinaryFormatter
  33.                                 BinaryFormatter bin = new BinaryFormatter();
  34.                                 // serialize the collection (EmployeeList1) to file (stream)
  35.                                 bin.Serialize(stream, EmployeeList1);
  36.                             }
  37.                         }
  38.                         catch (IOException)
  39.                         {
  40.                         }
  41.                         break;
  42.  
  43.                     case "r": // read the collection of object
  44.                         try
  45.                         {
  46.                             using (Stream stream = File.Open("data.bin", FileMode.Open))
  47.                             {
  48.                                 // create BinaryFormatter
  49.                                 BinaryFormatter bin = new BinaryFormatter();
  50.                                 // deserialize the collection (Employee) from file (stream)
  51.                                 var EmployeeList2 = (List<Employee>)bin.Deserialize(stream);
  52.  
  53.                                 // display the results
  54.                                 foreach (Employee Employee in EmployeeList2)
  55.                                 {
  56.                                     Console.WriteLine("{0}, {1}, {2}",
  57.                                         Employee.id,
  58.                                         Employee.fName,
  59.                                         Employee.sName);
  60.                                 }
  61.                             }
  62.                         }
  63.                         catch (IOException)
  64.                         {
  65.                         }
  66.                         break;
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     // The object we are going to Serializing/De-serializing
  73.     [Serializable()]
  74.     class Employee
  75.     {
  76.         public int id { get; set; }
  77.         public string fName { get; set; }
  78.         public string sName { get; set; }
  79.  
  80.         public Employee(int i, string f, string s)
  81.         {
  82.             id = i;
  83.             fName = f;
  84.             sName = s;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement