Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- // Note: When building this code, you must reference the
- // System.Runtime.Serialization.Formatters.Soap.dll assembly.
- using System.Runtime.Serialization.Formatters.Soap;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create a hashtable of values that will eventually be serialized.
- Hashtable EmployeeList1 = new Hashtable();
- EmployeeList1.Add(1, new Employee(1, "Elenor", "Scheller"));
- EmployeeList1.Add(2, new Employee(2, "Kaila", "Wilburn"));
- EmployeeList1.Add(3, new Employee(3, "Shea", "Wallen"));
- EmployeeList1.Add(4, new Employee(4, "Elliot", "Shaffer"));
- EmployeeList1.Add(5, new Employee(5, "Dominica", "Charney"));
- Serialize(EmployeeList1);
- Hashtable EmployeeList2 = new Hashtable(Deserialize());
- // display the results
- foreach (Employee Employee in EmployeeList2.Values)
- {
- Console.WriteLine("{0}, {1}, {2}",
- Employee.id,
- Employee.fName,
- Employee.sName);
- }
- Console.ReadLine();
- }
- private static void Serialize(Hashtable EmployeeList)
- {
- // Use a file stream here.
- using (FileStream fs = new FileStream("Test.soap", FileMode.Create))
- {
- // Construct a SoapFormatter and use it
- // to serialize the data to the stream.
- SoapFormatter formatter = new SoapFormatter();
- try
- {
- // Serialize EmployeeList to the file stream
- formatter.Serialize(fs, EmployeeList);
- }
- catch (Exception e)
- {
- Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", e.Message));
- }
- }
- }
- private static Hashtable Deserialize()
- {
- // Ctreate the return Hashtable
- Hashtable EmployeeList2=new Hashtable();
- // Open the file containing the data that you want to deserialize.
- using (FileStream fs = new FileStream("Test.soap", FileMode.Open))
- {
- // Construct a SoapFormatter and use it
- // to serialize the data from the stream.
- SoapFormatter formatter = new SoapFormatter();
- try
- {
- // Deserialize the hashtable from the file
- EmployeeList2 = (Hashtable)formatter.Deserialize(fs);
- }
- catch (Exception e)
- {
- Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", e.Message));
- }
- }
- // return the Deserialized data.
- return EmployeeList2;
- }
- }
- // The object we are going to Serializing/De-serializing
- [Serializable()]
- class Employee
- {
- public int id { get; set; }
- public string fName { get; set; }
- public string sName { get; set; }
- public Employee(int i, string f, string s)
- {
- id = i;
- fName = f;
- sName = s;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement