Advertisement
NAK

Serialization XmlSerializer (C#)

NAK
Dec 9th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml.Serialization;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.             // Create a List of values that will eventually be serialized.
  16.             List<Employee> EmployeeList1 = new List<Employee>();
  17.  
  18.             EmployeeList1.Add( new Employee(1, "Elenor", "Scheller"));
  19.             EmployeeList1.Add(new Employee(2, "Kaila", "Wilburn"));
  20.             EmployeeList1.Add( new Employee(3, "Shea", "Wallen"));
  21.             EmployeeList1.Add( new Employee(4, "Elliot", "Shaffer"));
  22.             EmployeeList1.Add(new Employee(5, "Dominica", "Charney"));
  23.  
  24.             Serialize(EmployeeList1);
  25.             List<Employee> EmployeeList2 = Deserialize();
  26.  
  27.             // display the results
  28.             foreach (Employee Employee in EmployeeList2)
  29.             {
  30.                 Console.WriteLine("{0}, {1}, {2}",
  31.                     Employee.id,
  32.                     Employee.fName,
  33.                     Employee.sName);
  34.             }
  35.             Console.ReadLine();
  36.         }
  37.  
  38.         private static void Serialize(List<Employee> EmployeeList)
  39.         {
  40.  
  41.             // Use a file stream here.
  42.             using (TextWriter WriteFileStream = new StreamWriter("test.xml"))
  43.             {
  44.                 // Construct a SoapFormatter and use it  
  45.                 // to serialize the data to the stream.
  46.                 XmlSerializer SerializerObj = new XmlSerializer(typeof(List<Employee>));
  47.  
  48.                 try
  49.                 {
  50.                     // Serialize EmployeeList to the file stream
  51.                     SerializerObj.Serialize(WriteFileStream, EmployeeList);
  52.                 }
  53.                 catch (Exception ex)
  54.                 {
  55.                     Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
  56.                 }
  57.             }
  58.         }
  59.  
  60.         private static List<Employee> Deserialize()
  61.         {
  62.             List<Employee> EmployeeList2 = new List<Employee>();
  63.  
  64.            // Create a new file stream for reading the XML file
  65.             using (FileStream ReadFileStream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
  66.             {
  67.                 // Construct a XmlSerializer and use it  
  68.                 // to serialize the data from the stream.
  69.                 XmlSerializer SerializerObj = new XmlSerializer(typeof(List<Employee>));
  70.                 try
  71.                 {
  72.                     // Deserialize the hashtable from the file
  73.                     EmployeeList2 = (List<Employee>)SerializerObj.Deserialize(ReadFileStream);
  74.                 }
  75.                 catch (Exception ex)
  76.                 {
  77.                     Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
  78.                 }
  79.  
  80.             }
  81.             // return the Deserialized data.
  82.             return EmployeeList2;
  83.         }
  84.     }
  85.  
  86.     // The object we are going to Serializing/De-serializing
  87.     [Serializable()]
  88.     public class Employee
  89.     {
  90.         public int id { get; set; }
  91.         public string fName { get; set; }
  92.         public string sName { get; set; }
  93.  
  94.         public Employee()
  95.         {
  96.         }
  97.  
  98.         public Employee(int i, string f, string s)
  99.         {
  100.             id = i;
  101.             fName = f;
  102.             sName = s;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement