Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Collections
- Imports System.Runtime.Serialization
- ' Note: When building this code, you must reference the
- ' System.Runtime.Serialization.Formatters.Soap.dll assembly.
- Imports System.Runtime.Serialization.Formatters.Soap
- Module Module1
- Sub Main()
- ' Create a hashtable of values that will eventually be serialized.
- Dim EmployeeList1 As 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)
- Dim EmployeeList2 As New Hashtable(Deserialize())
- ' display the results
- For Each Employee In EmployeeList2.Values
- Console.WriteLine("{0}, {1}, {2}",
- Employee.id,
- Employee.fName,
- Employee.sName)
- Next
- Console.ReadLine()
- End Sub
- Sub Serialize(EmployeeList As Hashtable)
- ' Use a file stream here.
- Using fs As New FileStream("Test.soap", FileMode.Create)
- ' Construct a SoapFormatter and use it
- ' to serialize the data to the stream.
- Dim formatter As New SoapFormatter
- Try
- ' Serialize EmployeeList to the file stream
- formatter.Serialize(fs, EmployeeList)
- Catch e As SerializationException
- Console.WriteLine("Failed to serialize. Reason: " & e.Message)
- End Try
- End Using
- End Sub
- Function Deserialize() As Hashtable
- ' Ctreate the return Hashtable
- Dim EmployeeList2 As New Hashtable
- ' Open the file containing the data that you want to deserialize.
- Using fs As New FileStream("Test.soap", FileMode.Open)
- ' Construct a SoapFormatter and use it
- ' to serialize the data from the stream.
- Dim formatter As New SoapFormatter
- Try
- ' Deserialize the hashtable from the file
- EmployeeList2 = DirectCast(formatter.Deserialize(fs), Hashtable)
- Catch e As SerializationException
- Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
- End Try
- End Using
- ' return the Deserialized data.
- Return EmployeeList2
- End Function
- ' The object we are going to Serializing/De-serializing
- <Serializable()>
- Class Employee
- Property id As Integer
- Property fName As String
- Property sName As String
- Public Sub New(i As Integer, f As String, s As String)
- id = i
- fName = f
- sName = s
- End Sub
- End Class
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement