1

I'm searching a way to serialize a structure that contains different kind of object such as Strings, Integer and Collections.

I have this data structure:

Dim database as New mainStruct

<Serializable()> Structure mainStruct
    Public name As String
    Public address As String
    Public Shared bookings As IList(Of booking) = New List(Of booking)
End Structure

<Serializable()> Structure booking
    Public id As Integer
    Public category As String
    Public description As String
End Structure

running the serializer:

    Dim fs As FileStream = New FileStream("x.bin", FileMode.OpenOrCreate)
    Dim serial As New XmlSerializer(GetType(mainStruct))
    serial.Serialize(fs, database)
    fs.Close()

the output only contains all non-collection variables such as:

<?xml version="1.0"?>
  <mainStruct xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <name>foo</name>
  <address>bar</address>
</mainStruct>

How i can serialize bookings Collection, without creating separate files using GetType(List(Of booking))?

Thanks!

Dandux97
  • 13
  • 2

1 Answers1

1

Shared members are not serialized because serialization is all about saving a class/structure instance, and shared members are not part of an instance.

Add an instance property to your structure and you should be good to go:

Public Property bookingList As IList(Of booking)
    Get
        Return mainStruct.bookings
    End Get
    Set(value As IList(Of booking)
        mainStruct.bookings = value
    End Set
End Property

If you want the tag to be called <bookings> you can just apply an XmlArray attribute to the property:

<XmlArray("bookings")> _
Public Property bookingList As IList(Of booking)
    ...same code as above...
End Property
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75