Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Serialize help

Serialize help

Scheduled Pinned Locked Moved Visual Basic
helpquestionxml
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrBean
    wrote on last edited by
    #1

    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 ?

    D 1 Reply Last reply
    0
    • M MrBean

      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 ?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        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 !!

        M 1 Reply Last reply
        0
        • A Anonymous

          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 !!

          M Offline
          M Offline
          MrBean
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • M MrBean

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • D Dave Kreskowiak

              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

              M Offline
              M Offline
              MrBean
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • M MrBean

                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

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups