Click here to Skip to main content
14,838,357 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need help on serializing /deserializing a class.
My class:
VB
<Serializable()>
Public Class Scanner
    Public Property ScannerID As String
    Public Property ScannerText As String

    Private Sub New() ' Vigtigt ellers er det ikke muligt at serialize

    End Sub

    Public Sub New(ByVal sScanID As String, ByVal sScanText As String)
        ScannerID = sScanID
        ScannerText = sScanText
    End Sub

    Public Overrides Function toString() As String
        Return ScannerID.ToString & ", " & ScannerText.ToString
    End Function
End Class

In a module
VB
Public lstScanner As New List(Of Scanner)

I use the list as Datasource for a ComboBox
The list is filled from a ComboBox text and a TextBox
VB
lstScanner.Add(New Scanner(cboScanID.Text, txtScanText.Text))

My goal is to save the data on application exit and reuse them on application start.
I succeed to save the data but cannot figure out how to deserialize them.
VB
Public Sub SaveScanner(ByVal pathFile As String, ByVal className As List(Of Scanner)) 'className As String)
        'Serialize object (class) to an XML file, via the use of StreamWriter

        Dim objStreamWriter As New StreamWriter(pathFile)
        Dim xsSerialize As New XmlSerializer(className.GetType) 'Determine what object types are present

        xsSerialize.Serialize(objStreamWriter, className) 'Save
        objStreamWriter.Close() 'Close File
    End Sub
Thank you in advance


What I have tried:

VB
Public Sub SaveScanner(ByVal pathFile As String, ByVal className As List(Of Scanner)) 'className As String)
    'Serialize object (class) to an XML file, via the use of StreamWriter

    Dim objStreamWriter As New StreamWriter(pathFile)
    Dim xsSerialize As New XmlSerializer(className.GetType) 'Determine what object types are present

    xsSerialize.Serialize(objStreamWriter, className) 'Save
    objStreamWriter.Close() 'Close File
End Sub
Posted
Updated 2-Nov-20 3:18am

Plenty of examples in the documentation:
Examples of XML Serialization | Microsoft Docs[^]
VB.NET
Dim xsSerialize As New XmlSerializer(GetType(List(Of Scanner)))
Using fs As Stream = IO.File.OpenRead(path)
    Return DirectCast(xsSerialize.Deserialize(fs), List(Of Scanner))
End Using
   
Comments
N. Henrik Lauridsen 2-Nov-20 11:52am
   
Hi Richard,
Got it working. Thanks a lot.
You can't serialize a collection directly; it needs a "parent" / root. e.g. (in c#)

C#
[DataContract()]
public class FengShuiStorage : IExtensibleDataObject {

   public ExtensionDataObject ExtensionData { get; set; }

   [DataMember()]
   public List<PersonDetails> Persons { get; set; } = new List<PersonDetails>();

   [DataMember()]
   public DateTime LastModified { get; set; }

}  // end class.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900