Tom Knowlton
asked on
Help with XML Serialization, please
I need some help getting XML Serialization to work.
I have this dumb 2D board game I am writing. My first serious attempt at a game.
I had someone helping me with the game. This person was more experienced in C# than I was. He recommended I use XML Serialization to SAVE the game and then LOAD the game. Seemed reasonable to me.
Unfortunately that person is no where to be found now.
So now I am stuck trying to finish off the XML Serialization strategy he started for saving the game........but I do not know serialization well enough to debug it or even ask the RIGHT QUESTIONS.
I am lost.
IF IT HELPS....the entire game source code is located here:
http://www.knowltonsoftware.com (just click on the GAME SOURCE DOWNLOAD link)
Here is the relevant module, GameStore.cs:
using System;
using System.Collections;
using System.Runtime.Serializati on;
using System.Xml.Serialization;
namespace Robotz
{
[XmlRoot("GamePlayer"), Serializable]
public class GameMove
{
private int _fromX;
private int _fromY;
private int _toX;
private int _toY;
private int _prevMovementPoints;
public GameMove()
{
}
[XmlAttribute("PrevMovePoi nts")]
public int PrevMovePoints
{
get{return _prevMovementPoints;}
}
[XmlAttribute("FromX")]
public int FromX
{
get{return _fromX;}
}
[XmlAttribute("FromY")]
public int FromY
{
get{return _fromY;}
}
[XmlAttribute("ToX")]
public int ToX
{
get{return _toX;}
}
[XmlAttribute("ToY")]
public int ToY
{
get{return _toY;}
}
public GameMove(int fromX,int fromY,int toX,int toY, int prevMovementPoints)
{
_fromX = fromX;
_fromY = fromY;
_toX = toX;
_toY = toY;
_prevMovementPoints = prevMovementPoints;
}
public void PlayMove(GameWorld gw)
{
gw.MovePiece(_fromX,_fromY ,_toX,_toY );
}
}
[XmlRoot("GameTurn"), Serializable]
public class GameTurn
{
private ArrayList _moves;
[XmlElement("move")]
public Robotz.GameMove[] GameMove
{
get
{
Robotz.GameMove[] GameMove = new Robotz.GameMove[_moves.Cou nt];
_moves.CopyTo(GameMove);
return GameMove;
}
set
{
if(value == null) return;
Robotz.GameMove[] movea = (Robotz.GameMove[])value;
_moves.Clear();
foreach(Robotz.GameMove gm in movea)
_moves.Add(gm);
}
}
// [XmlArray]
public ArrayList Moves
{
get{return _moves;}
}
public GameTurn()
{
_moves = new ArrayList();
}
public void AddMove(GameMove gm)
{
_moves.Add(gm);
}
public int DeleteLastMove() //Returns previous movement points for last move made
{
int prevMovementPoint = 0;
if (_moves.Count > 0)
{
prevMovementPoint = ((GameMove)_moves[_moves.C ount - 1]).PrevMovePoints;
_moves.RemoveAt(_moves.Cou nt - 1);
}
return prevMovementPoint;
}
public bool CanUndo
{
get {return _moves.Count > 0;}
}
public void PlayTurn(GameWorld gw)
{
foreach (GameMove gm in _moves)
{
gm.PlayMove(gw);
}
}
}
[XmlRoot("GameStore"), Serializable]
public class GameStore
{
private GamePlayer _playerOne;
private GamePlayer _playerTwo;
private ArrayList _gameTurns;
public GameStore()
{
}
[XmlElement("turn")]
public Robotz.GameTurn[] GameTurn
{
get
{
Robotz.GameTurn[] GameTurn = new Robotz.GameTurn[_gameTurns .Count];
_gameTurns.CopyTo(GameTurn );
return GameTurn;
}
set
{
if(value == null) return;
Robotz.GameTurn[] turnsa = (Robotz.GameTurn[])value;
_gameTurns.Clear();
foreach(Robotz.GameTurn ta in turnsa)
_gameTurns.Add(ta);
}
}
public ArrayList Turns
{
get{return _gameTurns;}
}
[XmlAttribute("GameTurnCou nt")]
public int TurnCount
{
get{return _gameTurns.Count;}
}
[XmlAttribute("GameTurnPla yerOne")]
public GamePlayer GetPlayerOne
{
get{return _playerOne;}
}
[XmlAttribute("GameTurnPla yerTwo")]
public GamePlayer GetPlayerTwo
{
get{return _playerTwo;}
}
public GamePlayer GetCurrentPlayer
{
get
{
if((_gameTurns.Count % 2) == 0)
{
return GetPlayerOne;
}
else
{
return GetPlayerTwo;
}
}
}
public GameStore(GamePlayer playerOne, GamePlayer playerTwo)
{
_playerOne = playerOne;
_playerTwo = playerTwo;
_gameTurns = new ArrayList();
}
public void PlayAllTurns(GameWorld gw)
{
gw.InitGamePieceArray();
gw.PointsLeft = 5; //starting points
foreach (GameTurn gt in _gameTurns)
{
gt.PlayTurn(gw);
gw.PointsLeft = 10; //Next turn points available
}
}
public void AddTurn(GameTurn gt)
{
_gameTurns.Add(gt);
}
public bool CanReplay
{
get{return (_gameTurns.Count > 0);}
}
}
}
Here is the resultant XML file output (BUT NOTICE NO VALUES ARE BEING OUTPUT):
<?xml version="1.0" encoding="utf-8" ?>
- <GameStore xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <turn>
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</turn>
- <turn>
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</turn>
- <Turns>
- <anyType xsi:type="GameTurn">
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</anyType>
- <anyType xsi:type="GameTurn">
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</anyType>
</Turns>
</GameStore>
I have this dumb 2D board game I am writing. My first serious attempt at a game.
I had someone helping me with the game. This person was more experienced in C# than I was. He recommended I use XML Serialization to SAVE the game and then LOAD the game. Seemed reasonable to me.
Unfortunately that person is no where to be found now.
So now I am stuck trying to finish off the XML Serialization strategy he started for saving the game........but I do not know serialization well enough to debug it or even ask the RIGHT QUESTIONS.
I am lost.
IF IT HELPS....the entire game source code is located here:
http://www.knowltonsoftware.com (just click on the GAME SOURCE DOWNLOAD link)
Here is the relevant module, GameStore.cs:
using System;
using System.Collections;
using System.Runtime.Serializati
using System.Xml.Serialization;
namespace Robotz
{
[XmlRoot("GamePlayer"), Serializable]
public class GameMove
{
private int _fromX;
private int _fromY;
private int _toX;
private int _toY;
private int _prevMovementPoints;
public GameMove()
{
}
[XmlAttribute("PrevMovePoi
public int PrevMovePoints
{
get{return _prevMovementPoints;}
}
[XmlAttribute("FromX")]
public int FromX
{
get{return _fromX;}
}
[XmlAttribute("FromY")]
public int FromY
{
get{return _fromY;}
}
[XmlAttribute("ToX")]
public int ToX
{
get{return _toX;}
}
[XmlAttribute("ToY")]
public int ToY
{
get{return _toY;}
}
public GameMove(int fromX,int fromY,int toX,int toY, int prevMovementPoints)
{
_fromX = fromX;
_fromY = fromY;
_toX = toX;
_toY = toY;
_prevMovementPoints = prevMovementPoints;
}
public void PlayMove(GameWorld gw)
{
gw.MovePiece(_fromX,_fromY
}
}
[XmlRoot("GameTurn"), Serializable]
public class GameTurn
{
private ArrayList _moves;
[XmlElement("move")]
public Robotz.GameMove[] GameMove
{
get
{
Robotz.GameMove[] GameMove = new Robotz.GameMove[_moves.Cou
_moves.CopyTo(GameMove);
return GameMove;
}
set
{
if(value == null) return;
Robotz.GameMove[] movea = (Robotz.GameMove[])value;
_moves.Clear();
foreach(Robotz.GameMove gm in movea)
_moves.Add(gm);
}
}
// [XmlArray]
public ArrayList Moves
{
get{return _moves;}
}
public GameTurn()
{
_moves = new ArrayList();
}
public void AddMove(GameMove gm)
{
_moves.Add(gm);
}
public int DeleteLastMove() //Returns previous movement points for last move made
{
int prevMovementPoint = 0;
if (_moves.Count > 0)
{
prevMovementPoint = ((GameMove)_moves[_moves.C
_moves.RemoveAt(_moves.Cou
}
return prevMovementPoint;
}
public bool CanUndo
{
get {return _moves.Count > 0;}
}
public void PlayTurn(GameWorld gw)
{
foreach (GameMove gm in _moves)
{
gm.PlayMove(gw);
}
}
}
[XmlRoot("GameStore"), Serializable]
public class GameStore
{
private GamePlayer _playerOne;
private GamePlayer _playerTwo;
private ArrayList _gameTurns;
public GameStore()
{
}
[XmlElement("turn")]
public Robotz.GameTurn[] GameTurn
{
get
{
Robotz.GameTurn[] GameTurn = new Robotz.GameTurn[_gameTurns
_gameTurns.CopyTo(GameTurn
return GameTurn;
}
set
{
if(value == null) return;
Robotz.GameTurn[] turnsa = (Robotz.GameTurn[])value;
_gameTurns.Clear();
foreach(Robotz.GameTurn ta in turnsa)
_gameTurns.Add(ta);
}
}
public ArrayList Turns
{
get{return _gameTurns;}
}
[XmlAttribute("GameTurnCou
public int TurnCount
{
get{return _gameTurns.Count;}
}
[XmlAttribute("GameTurnPla
public GamePlayer GetPlayerOne
{
get{return _playerOne;}
}
[XmlAttribute("GameTurnPla
public GamePlayer GetPlayerTwo
{
get{return _playerTwo;}
}
public GamePlayer GetCurrentPlayer
{
get
{
if((_gameTurns.Count % 2) == 0)
{
return GetPlayerOne;
}
else
{
return GetPlayerTwo;
}
}
}
public GameStore(GamePlayer playerOne, GamePlayer playerTwo)
{
_playerOne = playerOne;
_playerTwo = playerTwo;
_gameTurns = new ArrayList();
}
public void PlayAllTurns(GameWorld gw)
{
gw.InitGamePieceArray();
gw.PointsLeft = 5; //starting points
foreach (GameTurn gt in _gameTurns)
{
gt.PlayTurn(gw);
gw.PointsLeft = 10; //Next turn points available
}
}
public void AddTurn(GameTurn gt)
{
_gameTurns.Add(gt);
}
public bool CanReplay
{
get{return (_gameTurns.Count > 0);}
}
}
}
Here is the resultant XML file output (BUT NOTICE NO VALUES ARE BEING OUTPUT):
<?xml version="1.0" encoding="utf-8" ?>
- <GameStore xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <turn>
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</turn>
- <turn>
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</turn>
- <Turns>
- <anyType xsi:type="GameTurn">
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</anyType>
- <anyType xsi:type="GameTurn">
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</anyType>
</Turns>
</GameStore>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Additionally, while part of this could be attributed to my not reviewing the code in its entirety, I think its relatively safe to say your classes are quite poorly designed. I also don't think you're approaching the serialization issue from the correct angle. Like I said previously, I haven't reviewed the code in its entirety, but being a 2D board game I assume the players/characters/items/w