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
- InitialDirectio n
- 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(Elemen tName:="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(Eleme ntName:="Scenes "),
XmlArrayItem(El ementName:="Sce ne", Type:=GetType(E nvironments.Sce ne))>
_
Public Property Scenes() As Generic.List(Of Environments.Sc ene)
Get
Return pScenes
End Get
Set(ByVal value As Generic.List(Of Environments.Sc ene))
pScenes = value
End Set
End Property
End Class
SCENE.VB (Complete code)
Imports System.Xml.Seri alization
''' <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">Nam e 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(Ele mentName:="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(Ele mentName:="Code Name")_
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(Eleme ntName:="Parts" ), XmlArrayItem(El ementName:="Par t",
Type:=GetType(P art))_
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.Seri alization
''' <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.Figth er
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">Nam e of the part being displayed</param>
''' <param name="CodeName" >Codename of the part being
displayed</param>
''' <param name="Descripti on">Descripti on 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(Ele mentName:="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(Ele mentName:="Code Name")_
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(Ele mentName:="Desc ription")_
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(Eleme ntName:="Direct ions"),
XmlArrayItem(El ementName:="Dir ection", Type:=GetType(D irection))_
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(Ele mentName:="Enco unter")_
Public Property Encounter() As Creatures.Figth er
Get
Return pEncounter
End Get
Set(ByVal value As Creatures.Figth er)
pEncounter = value
End Set
End Property
''' <summary>
''' Contains the environment options of this part
''' </summary>
<XmlElement(Ele mentName:="Opti ons")_
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.Seri alization
''' <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">Co dename of the scene to go to</param>
''' <param name="Part">Cod ename of the part to go to</param>
''' <param name="Name">Nam e 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(Ele mentName:="Scen e")_
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(Ele mentName:="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(Ele mentName:="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