Advertisement
NAK

Serialization XmlSerializer (VB.NET)

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