3

Sorry if this question seems silly. I have studied a few web pages on the subject but am unsure how relevant they are to my issue. I have a 2D map designer that i have made. I would like to save the map, so that i can load it and play a game on it. I'm very new to Serialization, and i was wondering what documents i should study and if someone could point me to some web pages that relate to my serialization task. Here is the data structure:

    public class ModelMap
    {
        public ModelMap()
        {
            this.cellBgImage = new Image();
            this.exitImage = new Image();
            this.theseus = new Image();
            this.minotaur = new Image();
        }

        public int rows { get; set; }
        public int cols { get; set; }
        public int boardXPos { get; set; }
        public int boardYPos { get; set; }
        public int myCellSize { get; set; }
        public Image cellBgImage { get; set; }
        public string HorizontalWallSource { get; set; }
        public string VerticalWallSource { get; set; }
        public Image minotaur { get; set; }
        public Image theseus { get; set; }
        public Image exitImage { get; set; }
        public string exitCellPlacement { get; set; }
        public int myWidth { get; set; }
        public int myHeight { get; set; }
        private List<Cell> m_cells = new List<Cell>();
        public List<Cell> myCells
        {
            get
            {
                return m_cells;
            }
            set
            {
                m_cells = value;
            }
        }
    }
}

Cell Class:

public class Cell
    {
        public Cell(int col, int row, CellSide rightWall, CellSide bottomWall, ModelMap map)
        {
            this.myColumn = col;
            this.myRow = row;
            this.myRightWall = rightWall;
            this.myBottomWall = bottomWall;
            this.myMap = map;
        }

        public Cell(int col, int row, CellSide rightWall, CellSide bottomWall, ModelMap map, Image bgImage)
        {
            this.myColumn = col;
            this.myRow = row;
            this.myRightWall = rightWall;
            this.myBottomWall = bottomWall;
            this.myMap = map;
            this.myBgImage = bgImage;
        }

        public ModelMap myMap { get; set; }
        public Image myBgImage { get; set; }
        public bool hasMinotaur { get; set; }
        public bool hasTheseus { get; set; }
        public bool isExit { get; set; }
        public int mySize { get; set; }
        public CellSide myRightWall { get; set; }
        public CellSide myBottomWall { get; set; }
        public int myColumn { get; set; }
        public int myRow { get; set; }
    }

    public class CellSide
    {
        public CellSide(int hasWall, bool highlighted)
        {
            this.hasWall = hasWall;
            this.isHighlighted = highlighted;
        }

        public bool isHighlighted { get; set; }
        public int hasWall { get; set; }
    }
}

Thank you.

user2602079
  • 1,283
  • 5
  • 20
  • 39
  • Your model is OK and should have no problem serializing, except for the fact that you can't serialize the UI. Remove the `Image` properties and use a byte array or something to store your image data. – Federico Berasategui Nov 17 '13 at 22:07

1 Answers1

7

Read about standard .NET serialization and examples of XML serialization. You will need to make some changes to make serialization of those classes possible:

  • Cell and CellSide must have parameterless constructors,
  • Image (or rather its implementation) can't be directly serialized, but you can make some workaround (like in this question) or just serialize file path to the images and load them after deserialization

Then you can serialize an object to the XML file:

var map = new ModelMap();
...
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ModelMap));
using (var writer = new StreamWriter(@"e:\test.xml"))
{
    serializer.Serialize(writer, map);
}
Community
  • 1
  • 1
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • Thanks! I will try and make those changes to my program. I read the document on .NET serialization. Do you think XML serialization is the more likely way to go, rather than binary serialization? – user2602079 Nov 17 '13 at 22:12
  • It all depends, binary serialization will hide details of the map and may contain bitmaps, but XML is more editable etc. But using binary serialization wouldn't change a lot in this code example, just use `BinaryFormatter` class similarly. You will also have to add `[Serializable]` attribute to the serialized classes. – Konrad Kokosa Nov 17 '13 at 22:27
  • Great :) For now i will study XML serialization and hopefully use that. It seems appropriate. Thanks. – user2602079 Nov 17 '13 at 23:36