Advertisement
NAK

Serialization SoapFormatter (C#)

NAK
Dec 9th, 2013
75
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;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. // Note: When building this code, you must reference the
  9. // System.Runtime.Serialization.Formatters.Soap.dll assembly.
  10. using System.Runtime.Serialization.Formatters.Soap;
  11.  
  12.  
  13. namespace ConsoleApplication1
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.  
  20.             // Create a hashtable of values that will eventually be serialized.
  21.             Hashtable EmployeeList1 = new Hashtable();
  22.  
  23.             EmployeeList1.Add(1, new Employee(1, "Elenor", "Scheller"));
  24.             EmployeeList1.Add(2, new Employee(2, "Kaila", "Wilburn"));
  25.             EmployeeList1.Add(3, new Employee(3, "Shea", "Wallen"));
  26.             EmployeeList1.Add(4, new Employee(4, "Elliot", "Shaffer"));
  27.             EmployeeList1.Add(5, new Employee(5, "Dominica", "Charney"));
  28.  
  29.             Serialize(EmployeeList1);
  30.             Hashtable EmployeeList2 = new Hashtable(Deserialize());
  31.  
  32.             // display the results
  33.             foreach (Employee Employee in EmployeeList2.Values)
  34.             {
  35.                 Console.WriteLine("{0}, {1}, {2}",
  36.                     Employee.id,
  37.                     Employee.fName,
  38.                     Employee.sName);
  39.             }
  40.             Console.ReadLine();
  41.         }
  42.  
  43.         private static void Serialize(Hashtable EmployeeList)
  44.         {
  45.  
  46.             // Use a file stream here.
  47.             using (FileStream fs = new FileStream("Test.soap", FileMode.Create))
  48.             {
  49.                 // Construct a SoapFormatter and use it  
  50.                 // to serialize the data to the stream.
  51.                 SoapFormatter formatter = new SoapFormatter();
  52.                 try
  53.                 {
  54.                     // Serialize EmployeeList to the file stream
  55.                     formatter.Serialize(fs, EmployeeList);
  56.                 }
  57.                 catch (Exception e)
  58.                 {
  59.                     Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", e.Message));
  60.                 }
  61.             }
  62.         }
  63.  
  64.         private static Hashtable Deserialize()
  65.         {
  66.  
  67.             // Ctreate the return Hashtable
  68.             Hashtable EmployeeList2=new Hashtable();
  69.  
  70.             // Open the file containing the data that you want to deserialize.
  71.             using (FileStream fs = new FileStream("Test.soap", FileMode.Open))
  72.             {
  73.                 // Construct a SoapFormatter and use it  
  74.                 // to serialize the data from the stream.
  75.                 SoapFormatter formatter = new SoapFormatter();
  76.                 try
  77.                 {
  78.                     // Deserialize the hashtable from the file
  79.                     EmployeeList2 = (Hashtable)formatter.Deserialize(fs);
  80.                 }
  81.                 catch (Exception e)
  82.                 {
  83.                     Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", e.Message));
  84.                 }
  85.             }
  86.             // return the Deserialized data.
  87.             return EmployeeList2;
  88.         }
  89.  
  90.     }
  91.  
  92.     // The object we are going to Serializing/De-serializing
  93.     [Serializable()]
  94.     class Employee
  95.     {
  96.         public int id { get; set; }
  97.         public string fName { get; set; }
  98.         public string sName { get; set; }
  99.  
  100.         public Employee(int i, string f, string s)
  101.         {
  102.             id = i;
  103.             fName = f;
  104.             sName = s;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement