Serialize 3 levels of nested collections.


R

Rick Luckwell

I have 3 collections(reports, services, charts) of objects(report,
service, chart) that are nested with each other. When I serialize the
object the output only contains reports and services but not the
charts. Am I missing something? can you serialize to this depth without
extra code? or do I need to call the serialize method of the child
objects?

clsReports.SerializeObject("c:\report.xml", m_AllReports)
'"m_AllReports" contains the collections of object instances I am
trying to serialize

Each collection class is based on the following
<Serializable()> Public Class clsReports
Inherits System.Collections.CollectionBase

Default Public Property Item(ByVal index As Integer) As clsReport
Get
Return CType(Me.List(index), clsReport)
End Get
Set(ByVal value As clsReport)
Me.List(index) = value
End Set
End Property

Public Sub Add(ByVal value As clsReport)
Me.List.Add(value)
End Sub


Public Shared Sub SerializeObject(ByVal filename As String, ByVal
Report As clsReports)
Dim s As New XmlSerializer(GetType(clsReports))
Dim writer As TextWriter = New StreamWriter(filename)
s.Serialize(writer, Report)
writer.Close()
End Sub

Public Shared Function DeserializeObject(ByVal filename As String)
As clsReports
Dim fs As New IO.FileStream(filename, FileMode.Open)
Dim w As New
Xml.Serialization.XmlSerializer(GetType(clsReports))
Dim g As clsReports = CType(w.Deserialize(fs), clsReports)

fs.Close()

Return g
End Function

End Class

And each object is based on the following

<Serializable()> Public Class clsReport
Private m_ReportName As String = ""
Private m_ServList As New clsServices

Public Property Name() As String
Get
Return Me.m_ReportName
End Get
Set(ByVal value As String)
Me.m_ReportName = value
End Set
End Property

Public ReadOnly Property ServiceList() As clsServices
Get
Return Me.m_ServList
End Get
End Property

Public Sub AddService(ByVal NewSvc As clsService)
Me.m_ServList.Add(NewSvc)
End Sub
End Class
 
Ad

Advertisements

R

R. MacDonald

Hello, Rick,

Your question indicates that your "service" object contains a "chart"
object, but you haven't shown the code for clsService, so it's difficult
to guess why "chart" might not be being serialized. Perhaps if you can
post more code someone will be able to help.

Try to reduce the problem to the smallest possible executable example
and post the code for that. (Actually, in reducing the problem to the
smallest possible executable example the answer often makes itself clear.)

Cheers,
Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top