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

xml serialization using generic list fails

P: n/a
I've got a gaming framework i'm building and i want to save myself the
trouble of reading and writting the complete game data to a custom file
and load/save it to an XML file but i'm getting problem serializing my
stuff to XML when it comes to collections.

I'm currently using .net2 with generic lists to prevent users putting
all sorts of stuff in the arrays (Although im sure i'll be the only
user of the classes but not the game, anyway).

So i need to be able to serialize a complex object structure that looks
like this (simplified) :

Game
- Hero
- InitialDirection
- Scenes
- Parts
- Directions

I am able to serialize the Direction object or Hero object fine (Hero
object contains sub objects like weapon, armor and options but no
collection and everything serializes fine, but when i get to the Scenes
or Parts collection properties it simply fails stating that there was
an error serializing the Scene object.

I have empty constructors and the attributes seem to be set ok, so
here's the code for that seems to fail:

GAME.VB

''' <summary>
''' Represents a complete game you can play
''' </summary>
<XmlRoot(ElementName:="Game")_
Public Class Game

''' <summary>
''' Represents a complete game you can play
''' </summary>
Public Sub New()
End Sub

''' <summary>
''' Contains the scene in the game
''' </summary>
<XmlArray(ElementName:="Scenes"),
XmlArrayItem(ElementName:="Scene", Type:=GetType(Environments.Scene))>
_
Public Property Scenes() As Generic.List(Of Environments.Scene)
Get
Return pScenes
End Get
Set(ByVal value As Generic.List(Of Environments.Scene))
pScenes = value
End Set
End Property

End Class

SCENE.VB (Complete code)

Imports System.Xml.Serialization

''' <summary>
''' Englobes several parts to make up scenes in your game
''' </summary>
Public Class Scene

Private pName As String
Private pCodeName As String
Private pParts As New Generic.List(Of Part)

''' <summary>
''' Describes a scene that the user can go through
''' </summary>
Public Sub New()
End Sub

''' <summary>
''' Describes a scene that the user can go through
''' </summary>
''' <param name="Name">Name of the part being displayed</param>
''' <param name="CodeName">Codename of the part being
displayed</param>
Public Sub New(ByVal Name As String, ByVal CodeName As String)
Me.Name = Name
Me.CodeName = CodeName
End Sub

''' <summary>
''' Contains the visual name of the scene
''' </summary>
<XmlElement(ElementName:="Name")_
Public Property Name() As String
Get
Return pName
End Get
Set(ByVal value As String)
pName = value
End Set
End Property

''' <summary>
''' Contains the codename of this scene used in direction
references
''' </summary>
<XmlElement(ElementName:="CodeName")_
Public Property CodeName() As String
Get
Return pCodeName
End Get
Set(ByVal value As String)
pCodeName = value
End Set
End Property

''' <summary>
''' Contains the different directions available from this part to
another part
''' </summary>
<XmlArray(ElementName:="Parts"), XmlArrayItem(ElementName:="Part",
Type:=GetType(Part))_
Public Property Parts() As Generic.List(Of Part)
Get
Return pParts
End Get
Set(ByVal value As Generic.List(Of Part))
pParts = value
End Set
End Property

End Class

PART.VB

Imports System.Xml.Serialization

''' <summary>
''' Describes a part of a scene that the user can go through
''' </summary>
Public Class Part

Private pName As String
Private pCodeName As String
Private pDescription As String
Private pDirections As New Generic.List(Of Direction)
Private pEncounter As Creatures.Figther
Private pOptions As New Options()

''' <summary>
''' Describes a part of a scene that the user can go through
''' </summary>
Public Sub New()
End Sub

''' <summary>
''' Describes a part of a scene that the user can go through
''' </summary>
''' <param name="Name">Name of the part being displayed</param>
''' <param name="CodeName">Codename of the part being
displayed</param>
''' <param name="Description">Description of the part</param>
Public Sub New(ByVal Name As String, ByVal CodeName As String,
ByVal Description As String)
Me.Name = Name
Me.CodeName = CodeName
Me.Description = Description
End Sub

''' <summary>
''' Contains the visual name of the part
''' </summary>
<XmlElement(ElementName:="Name")_
Public Property Name() As String
Get
Return pName
End Get
Set(ByVal value As String)
pName = value
End Set
End Property

''' <summary>
''' Contains the codename of this part used in direction references
''' </summary>
<XmlElement(ElementName:="CodeName")_
Public Property CodeName() As String
Get
Return pCodeName
End Get
Set(ByVal value As String)
pCodeName = value
End Set
End Property

''' <summary>
''' Main description of events happening in this part
''' </summary>
<XmlElement(ElementName:="Description")_
Public Property Description() As String
Get
Return pDescription
End Get
Set(ByVal value As String)
pDescription = value
End Set
End Property

''' <summary>
''' Contains the different directions available from this part to
another part
''' </summary>
<XmlArray(ElementName:="Directions"),
XmlArrayItem(ElementName:="Direction", Type:=GetType(Direction))_
Public Property Directions() As Generic.List(Of Direction)
Get
Return pDirections
End Get
Set(ByVal value As Generic.List(Of Direction))
pDirections = value
End Set
End Property

''' <summary>
''' Contains the encounter if any
''' </summary>
<XmlElement(ElementName:="Encounter")_
Public Property Encounter() As Creatures.Figther
Get
Return pEncounter
End Get
Set(ByVal value As Creatures.Figther)
pEncounter = value
End Set
End Property

''' <summary>
''' Contains the environment options of this part
''' </summary>
<XmlElement(ElementName:="Options")_
Public Property Options() As Options
Get
Return pOptions
End Get
Set(ByVal value As Options)
pOptions = value
End Set
End Property

End Class

DIRECTION.VB

Imports System.Xml.Serialization

''' <summary>
''' Directs the user towards another portion of story
''' </summary>
Public Class Direction

Private pScene As String
Private pPart As String
Private pName As String

''' <summary>
''' Directs the user towards another portion of story
''' </summary>
Public Sub New()
End Sub

''' <summary>
''' Directs the user towards another portion of story
''' </summary>
''' <param name="Scene">Codename of the scene to go to</param>
''' <param name="Part">Codename of the part to go to</param>
''' <param name="Name">Name of this direction</param>
Public Sub New(ByVal Scene As String, ByVal Part As String, ByVal
Name As String)
Me.Scene = Scene
Me.Part = Part
Me.Name = Name
End Sub

''' <summary>
''' Codename of the scene to go to
''' </summary>
<XmlElement(ElementName:="Scene")_
Public Property Scene() As String
Get
Return pScene
End Get
Set(ByVal value As String)
pScene = value
End Set
End Property

''' <summary>
''' Codename of the part to go to
''' </summary>
<XmlElement(ElementName:="Part")_
Public Property Part() As String
Get
Return pPart
End Get
Set(ByVal value As String)
pPart = value
End Set
End Property

''' <summary>
''' Name of this direction
''' </summary>
<XmlElement(ElementName:="Name")_
Public Property Name() As String
Get
Return pName
End Get
Set(ByVal value As String)
pName = value
End Set
End Property

End Class
-------------------

So please just tell me what's wrong and i'll fix it, but sincerely, i'd
really like and apreciate a explanation so i can understand what is
wrong

Sep 10 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.