Serialize help
-
I'm looking into serializing some of my classess to/from XML and need some help. Imagine the scenario below (these are not my actual classes, but illustrate the problem)...
Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains MyReportObject objects End Class Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
How do I enable my two classes for serializing ? I have tried adding different XML attributes to the class members and the Serializable attribute to the classes :Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains several MyReportObject objects End Class Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
But I still get an error when I try to serialize the MyReport class ? What am I doing wrong ? -
I'm looking into serializing some of my classess to/from XML and need some help. Imagine the scenario below (these are not my actual classes, but illustrate the problem)...
Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains MyReportObject objects End Class Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
How do I enable my two classes for serializing ? I have tried adding different XML attributes to the class members and the Serializable attribute to the classes :Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains several MyReportObject objects End Class Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
But I still get an error when I try to serialize the MyReport class ? What am I doing wrong ?Your class isn't attributed Serializable and/or does not implement the ISerializable interface. You can't just tag your classes properties with the attributes you have and expect it to work. See the ISerializable Interface[^] docs for more information. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Your class isn't attributed Serializable and/or does not implement the ISerializable interface. You can't just tag your classes properties with the attributes you have and expect it to work. See the ISerializable Interface[^] docs for more information. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Sorry... actually, my classes are marked as serializable :
Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains several MyReportObject objects End Class Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
But somehow my code fails if the m_Objects arraylist contains objects MyReportObject - why ? I have tried a number of different combinations so the code above isn't 100% accurate, but should give a good idea of what I'm trying to do. Please help !! -
Sorry... actually, my classes are marked as serializable :
Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains several MyReportObject objects End Class Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
But somehow my code fails if the m_Objects arraylist contains objects MyReportObject - why ? I have tried a number of different combinations so the code above isn't 100% accurate, but should give a good idea of what I'm trying to do. Please help !!Sorry for the above post... the forum somehow didn't recognize my account ?!?!?! (grrr!) Anyway... the two classes ARE marked as
< Serializable() >
but not shown in the code above (again a forum bug ?) Here the code AGAIN with the Serializable tags :) ...< Serializable() > Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains several MyReportObject objects End Class < Serializable() > Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
-
Sorry for the above post... the forum somehow didn't recognize my account ?!?!?! (grrr!) Anyway... the two classes ARE marked as
< Serializable() >
but not shown in the code above (again a forum bug ?) Here the code AGAIN with the Serializable tags :) ...< Serializable() > Public Class MyReport Public m_Title as String Public m_BackColor as Color Public m_Objects as new ArrayList ' Contains several MyReportObject objects End Class < Serializable() > Public Class MyReportObject Public m_Text as String Public m_X as Integer Public m_Y as Integer End Class
Ok, so it is. It didn't come up before because the < and > brackets weren't typed in correctly. Not beacuse of a bug in CP's code. Now, what about your serialization code? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Ok, so it is. It didn't come up before because the < and > brackets weren't typed in correctly. Not beacuse of a bug in CP's code. Now, what about your serialization code? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Here's the current and complete test source including the test function :
Imports System.Xml.Serialization Public Class MyReport Public m_Title As String Public m_BackColor As Color Public m_Objects As New ArrayList ' Contains several MyReportObject objects End Class Public Class MyReportObject Public m_Text As String Public m_X As Integer Public m_Y As Integer End Class Module TestFunctions Public Sub Test() ' Making test data... Dim oRep As New MyReport oRep.m_Title = "Some new report..." oRep.m_BackColor = oRep.m_BackColor.WhiteSmoke Dim oObj As New MyReportObject oObj.m_Text = "A text..." oObj.m_X = 10 oObj.m_Y = 10 oRep.m_Objects.Add(oObj) ' Leave this line out, and serializeing below will succeed ! Try ' Serialize to XML file... Dim file As New System.io.FileStream("c:\MyReport.xml", IO.FileMode.Create) Dim ser As New System.Xml.Serialization.XmlSerializer(oRep.GetType) ser.Serialize(file, oRep) ser = Nothing file.Close() file = Nothing MsgBox("Done!") Catch ex As Exception MsgBox(ex.Message & vbCr & vbCr & ex.InnerException.Message) End Try End Sub End Module
-
Here's the current and complete test source including the test function :
Imports System.Xml.Serialization Public Class MyReport Public m_Title As String Public m_BackColor As Color Public m_Objects As New ArrayList ' Contains several MyReportObject objects End Class Public Class MyReportObject Public m_Text As String Public m_X As Integer Public m_Y As Integer End Class Module TestFunctions Public Sub Test() ' Making test data... Dim oRep As New MyReport oRep.m_Title = "Some new report..." oRep.m_BackColor = oRep.m_BackColor.WhiteSmoke Dim oObj As New MyReportObject oObj.m_Text = "A text..." oObj.m_X = 10 oObj.m_Y = 10 oRep.m_Objects.Add(oObj) ' Leave this line out, and serializeing below will succeed ! Try ' Serialize to XML file... Dim file As New System.io.FileStream("c:\MyReport.xml", IO.FileMode.Create) Dim ser As New System.Xml.Serialization.XmlSerializer(oRep.GetType) ser.Serialize(file, oRep) ser = Nothing file.Close() file = Nothing MsgBox("Done!") Catch ex As Exception MsgBox(ex.Message & vbCr & vbCr & ex.InnerException.Message) End Try End Sub End Module
OK. I didn't see this before, but since you have a complex object (MyReport) with a second level of serialization (ArrayList), the XML Formatter won't go into the ArrayList to serialize it. In other words, the XML Formatter is a Shallow formatter. This article, Object Serialization in Visual Basic .NET[^], on MSDN will show you a handful of different scenarios for serializing different objects. You can implement the ISerializable interface in your Report object and supply custom formatter code to get it to work with the XML Formatter. If you don;t want to do that, you'll have to switch to using one of the Deep formatters, either the Binary or SOAP formatters. I would suggest implementing the ISerializable interface. It'll give your Serialization experience a very useful boost for future projects. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome