By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Serialize 3 levels of nested collections.

P: n/a
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

Jul 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Rick Luckwell wrote:
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
Jul 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.