10 Replies - 1331 Views - Last Post: 05 April 2013 - 03:26 PM Rate Topic: -----

#1 Niklas62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-April 13

How to serialize multiple objects into the same file?

Posted 05 April 2013 - 09:03 AM

Hey, I am trying to serialize multiple objects of classes into the same XML file but it doesn't work. It works with the serialization part, but not when it is deserializing. I am not sure if i am doing it correctly, some help or guide would be appreciated.

    'Objects
    Private character as New Character
    Pirvate shopContainer as new ShopItems


    Private Sub Game_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DeserializeFromXML()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SerializeToXML()
    End Sub


    'XmlSerializers
    Private charDetails As New XmlSerializer(GetType(Character))
    Private shopDetails As New XmlSerializer(GetType(ShopItems))


    'Serializes the two objects to the same file at desktop.
    Private Sub SerializeToXML()
        Using writeStream As New StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/TESTXML.xml")
            charDetails.Serialize(writeStream, character)
            'shopDetails.Serialize(writeStream, ShopContainer)
        End Using
    End Sub

 
    Private Sub DeserializeFromXML()
            Using readStream As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/TESTXML.xml")
            ShopContainer = CType(shopDetails.Deserialize(readStream), ShopItems)
            character = CType(charDetails.Deserialize(readStream), Character)
            End Using
    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: How to serialize multiple objects into the same file?

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 09:27 AM

It's probably the structure of the class. Have you tried nesting? Since the inner classes belong to the outer class they will be serialized in same file and deserialized the same.

Public Class Outer
  Public Class Inner1
   ...
  End Class
  Public Class Inner2
   ...
  End Class
End Class


Diff approach here: Code Project Article.
Was This Post Helpful? 0
  • +
  • -

#3 Niklas62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-April 13

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 09:55 AM

Thanks _HAWK_ for a fast answer:)

Do you mean like putting the classes in one big class?
At the moment my project is built like this:
First of all I have all the forms in one projectfile. The classes are placed separately in a class library. Where each class is placed in its own class item.
So for example: I may have a form called "RobberyForm", its class is placed in the classitem "Robbery".

Is this the right way to do it? how can I do it differently?
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 10:08 AM

I don't know what you mean by Classitem.
Was This Post Helpful? 1
  • +
  • -

#5 Niklas62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-April 13

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 10:33 AM

Posted Image

Hehe, I am not sure if it's called class item:p
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 11:43 AM

It is just called a class.

Did you see how the example used multiple classes - some used a List(Of T)? This makes a single class object with other objects inside it. Then you fill this object with data and serialize.

If you only need simple data stored in the main class use Properties - they serialize. You can also use a List(Of T) for any type of data you need to store a collection of - they serialize.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2233
  • View blog
  • Posts: 9,401
  • Joined: 29-May 08

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 01:03 PM

You could create a class the encapsulates the state(s) of the game world

<Serializable()>
Public Class StateOfWorld

  Public Property Character As Character
  Public Property ShopItem As ShopItem
End Class


then serialize that.

Also you know that the Serialize and Deserialize methods can be made generic extension methods so it can output to (almost) any type.
See code example in blog post.

This post has been edited by AdamSpeight2008: 05 April 2013 - 01:04 PM

Was This Post Helpful? 1
  • +
  • -

#8 Niklas62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-April 13

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 01:17 PM

I know this is probably too much to ask for, but could you please give me a simple example. I am a fresh programmer and have a hard time understanding the one in the link.

The things I need serializing for in my project is to save three objects of classes(Public person as New Persons, Public robbery as new Robberies, Public character as new Characters) and upon that I want to save two arraylists into the same XML file.

View PostAdamSpeight2008, on 05 April 2013 - 01:03 PM, said:

You could create a class the encapsulates the state(s) of the game world

<Serializable()>
Public Class StateOfWorld

  Public Property Character As Character
  Public Property ShopItem As ShopItem
End Class


then serialize that.

Also you know that the Serialize and Deserialize methods can be made generic extension methods so it can output to (almost) any type.
See code example in blog post.


Oh didn't see your answer, before I posted mine. StateofWorld seems like a simple solution, will look into it.
Was This Post Helpful? 0
  • +
  • -

#9 Niklas62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-April 13

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 02:17 PM

Sorry guys I still have a hard time understand how to do it. Can you please give an example for the code below. This code has the same concept as in my project. So after you have clicked the button the objects gets their value, these should then be serialized to an XML file. The next time the project starts it deserializes in form_loading and the object recieves their value without clicking the button.

Public Class Form1
    Public characterObject As New Character
    Public shopObject As New Shop
    Public listShops As New ArrayList()


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        characterObject.charName = "Name"
        characterObject.charHeight = 55

        shopObject.ShopType = "Shop"
        shopObject.ShopName = "Shopname"

        listShops.Add(1)
        listShops.Add(2)
    End Sub


End Class

Public Class Character
    Public charName As String
    Public charHeight As Integer
End Class
Public Class Shop
    Public ShopType As String
    Public ShopName As String
End Class

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2233
  • View blog
  • Posts: 9,401
  • Joined: 29-May 08

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 02:52 PM

Did you examine the blog post example I previously linked to?
Was This Post Helpful? 0
  • +
  • -

#11 Niklas62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-April 13

Re: How to serialize multiple objects into the same file?

Posted 05 April 2013 - 03:26 PM

View PostAdamSpeight2008, on 05 April 2013 - 02:52 PM, said:

Did you examine the blog post example I previously linked to?

Yes I tried, but still don't get a clear picture of what to do. I am not familiar with everything on that post and the code seems more complicated than it probably is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1