• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

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.Serialization;
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("PrevMovePoints")]
            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.Count];
                        _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.Count - 1]).PrevMovePoints;
                        _moves.RemoveAt(_moves.Count - 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("GameTurnCount")]
            public int TurnCount
            {
                  get{return _gameTurns.Count;}
            }

            [XmlAttribute("GameTurnPlayerOne")]
            public GamePlayer GetPlayerOne
            {
                  get{return _playerOne;}
            }

            [XmlAttribute("GameTurnPlayerTwo")]
            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>
0
Tom Knowlton
Asked:
Tom Knowlton
  • 2
2 Solutions
 
BurntSkyCommented:
While I'm not quite sure exactly what your problem is I'm going to state the obvious and tell you that it would be wise to do some reading on Xml serialization.  I'll include a couple links that should help you understand exactly what the point of serialization is and how to use it.

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/whatever are placed on something resembling a grid and therefore have particular locations relative to said grid associated with them.  If this is so, the serialized class should contain only this and other important information.  It looks as though you're serializing entire sequences of moves that players/characters may make through a series of turns.  This may be part of your design (to allow backwards/forward or undo/redo movement support) but otherwise I see it as unnecessary.  I wrote a rather extensive game engine in Java a while back that I'd be willing to share with you if you're interested.
0
 
AgariciCommented:
i think the reason your properties are not serialized is they are read only
a read only property is not serialized because it can't be deserialized.

you see
[XmlElement("turn")]          
public Robotz.GameTurn[] GameTurn{get;set}
appears in the xml while
[XmlAttribute("GameTurnCount")]
public int TurnCount{get;}
does not


hth,
A.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now