Advertisement
NAK

Serialization DataContractSerializer (C#)

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