0

I've created three classes:

cLesson cStage cActivity

Right now, I'm experimenting with serialising list properties and have the following code for two of my classes setup:

cLesson.vb

<Serializable()> _
<System.Xml.Serialization.XmlInclude(GetType(List(Of cStage)))> _
Public Class cLesson
    Private lStages As New List(Of cStage)

    Public Sub addStage(sStage As String)
        Dim oStage As New cStage

        oStage.Title = sStage

        lStages.Add(oStage)
    End Sub

    Public Sub listStages()
        For Each oStage In lStages
            MsgBox(oStage.Title)
        Next
    End Sub
End Class

cStage.vb

<Serializable> _
Public Class cStage
    Private sTitle As String

    Public Property Title() As String
        Get
            Return sTitle
        End Get
        Set(ByVal Value As String)
            sTitle = Value
        End Set
    End Property
End Class

Form1.vb - Serialise Calls

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim xml_serializer As New Xml.Serialization.XmlSerializer(GetType(cLesson))

    'Word document pointers
    Dim pDoc As String

    'Open master document
    Dim fdFile As SaveFileDialog = New SaveFileDialog

    fdFile.Title = "Save Reading Resource"
    fdFile.Filter = "Dat Files|*.dat"
    fdFile.RestoreDirectory = True

    If fdFile.ShowDialog = Windows.Forms.DialogResult.OK Then
        pDoc = fdFile.FileName
        Dim stream_writer As New StreamWriter(pDoc, False)

        Try
            xml_serializer.Serialize(stream_writer, oLesson)
            stream_writer.Close() ' close the file
        Catch ex As Exception
            MsgBox(ex.InnerException.ToString)
        End Try
    End If
End Sub

However, when I run this program, I'm not getting the list property appearing in the XML.

In the end, I want a lesson with a list of stages, each stage of which can have a list of activities.

Any advice would be great appreciated. I've looked online a lot and haven't been able to find anything to help.

UPDATE 1

Making the list public allows it to be serialised. However, I'd prefer it to be private.

Public lStages As New List(Of cStage)

1 Answers1

-1

XML Serialization works only on fields and properties marked as Public. Quoting the official documentatin article Introducing XML Serialization:

XML serialization serializes only the public fields and property values of an object into an XML stream.

(...)

XML serialization does not convert methods, indexers, private fields, or read-only properties (except read-only collections). To serialize all an object's fields and properties, both public and private, use the DataContractSerializer instead of XML serialization.

Thus you can switch to the DataContractSerializer to serialize private fields as well, but for it to work with regular XML Serialization (which supports XML attributes) the only way would be to change your field's declaration to:

Public lStages As New List(Of cStage)
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75