Advertisement
NAK

Serialization SoapFormatter (VB.NET)

NAK
Dec 9th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.93 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Collections
  3. Imports System.Runtime.Serialization
  4.  
  5. ' Note: When building this code, you must reference the
  6. ' System.Runtime.Serialization.Formatters.Soap.dll assembly.
  7. Imports System.Runtime.Serialization.Formatters.Soap
  8.  
  9. Module Module1
  10.  
  11.     Sub Main()
  12.         ' Create a hashtable of values that will eventually be serialized.
  13.         Dim EmployeeList1 As New Hashtable
  14.  
  15.         EmployeeList1.Add(1, New Employee(1, "Elenor", "Scheller"))
  16.         EmployeeList1.Add(2, New Employee(2, "Kaila", "Wilburn"))
  17.         EmployeeList1.Add(3, New Employee(3, "Shea", "Wallen"))
  18.         EmployeeList1.Add(4, New Employee(4, "Elliot", "Shaffer"))
  19.         EmployeeList1.Add(5, New Employee(5, "Dominica", "Charney"))
  20.  
  21.         Serialize(EmployeeList1)
  22.         Dim EmployeeList2 As New Hashtable(Deserialize())
  23.  
  24.         ' display the results
  25.         For Each Employee In EmployeeList2.Values
  26.             Console.WriteLine("{0}, {1}, {2}",
  27.                 Employee.id,
  28.                 Employee.fName,
  29.                 Employee.sName)
  30.         Next
  31.         Console.ReadLine()
  32.     End Sub
  33.  
  34.     Sub Serialize(EmployeeList As Hashtable)
  35.  
  36.         ' Use a file stream here.
  37.         Using fs As New FileStream("Test.soap", FileMode.Create)
  38.  
  39.             ' Construct a SoapFormatter and use it  
  40.             ' to serialize the data to the stream.
  41.             Dim formatter As New SoapFormatter
  42.             Try
  43.                 ' Serialize EmployeeList to the file stream
  44.                 formatter.Serialize(fs, EmployeeList)
  45.             Catch e As SerializationException
  46.                 Console.WriteLine("Failed to serialize. Reason: " & e.Message)
  47.             End Try
  48.  
  49.         End Using
  50.     End Sub
  51.  
  52.     Function Deserialize() As Hashtable
  53.  
  54.         ' Ctreate the return Hashtable
  55.         Dim EmployeeList2 As New Hashtable
  56.  
  57.         ' Open the file containing the data that you want to deserialize.
  58.         Using fs As New FileStream("Test.soap", FileMode.Open)
  59.             ' Construct a SoapFormatter and use it  
  60.             ' to serialize the data from the stream.
  61.             Dim formatter As New SoapFormatter
  62.             Try
  63.  
  64.                 ' Deserialize the hashtable from the file  
  65.                 EmployeeList2 = DirectCast(formatter.Deserialize(fs), Hashtable)
  66.             Catch e As SerializationException
  67.                 Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
  68.             End Try
  69.         End Using
  70.         '  return the Deserialized data.
  71.         Return EmployeeList2
  72.  
  73.     End Function
  74.  
  75.     ' The object we are going to Serializing/De-serializing
  76.     <Serializable()>
  77.     Class Employee
  78.         Property id As Integer
  79.         Property fName As String
  80.         Property sName As String
  81.  
  82.         Public Sub New(i As Integer, f As String, s As String)
  83.             id = i
  84.             fName = f
  85.             sName = s
  86.         End Sub
  87.     End Class
  88.  
  89. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement