Deserialize XML to object for game editor

Hello! I’m trying to write a game editor. I can already load my Map class from my game project with Content.Load<>.

I’m running into issues trying to find a way to deserialize the XML file. I’m not too sure which method to use, but every one I try runs into one problem or another.

If I try to use the IntermediateSerializer.Deserialize method, it says it doesn’t support multi-dimentional arrays.

Trying to use the deserialize method of an XmlSerializer returns errors reflecting the type(Map)

This is the code for the Map class:
public class Map
{
#region Variables/Properties

        public string BGpath { get; set; }        

        public string collisionMaskFile;   


        [XmlArray("Events")]
        public List<Scene> Events;

        [XmlArray("Entities")]
        [XmlArrayItem("NPC", typeof(NPC))]
        public List<Entity> Entities;

        [XmlArray("MapObjects")]
        public List<MapObject> MapObjects;
        #endregion

        //public List<MapLayer> Layers { get; set; }

        //public CollisionLayer collisionLayer;

    }

Example of one of the XML files without much data in it:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:Practice_RPG="Practice_RPG">
  <Asset Type="Practice_RPG:Map">
    <BGpath>C:\STUFF</BGpath>
    <collisionMaskFile>TestText</collisionMaskFile>
    <Events>    </Events>
    <Entities></Entities>
    <MapObjects> </MapObjects>
  </Asset>
</XnaContent>

Any help would be appreciated.

Support for serializing multi-dimensional arrays was added after the 3.6 release.

So things should work in a development build :slight_smile:

I updated to the latest developers build but am still running into the error: “We only support single dimension arrays.”
It’s likely I’m doing something wrong though.

XmlReaderSettings settings = new XmlReaderSettings();
 using (XmlReader reader = XmlReader.Create(fileName, settings))
            {
                map = IntermediateSerializer.Deserialize<Map>(reader, null);
            }

Above is the code I’m using to try to deserialize my XML file. If anyone see’s anything wrong I’d appreciate it. :slight_smile:

Oh, I was wrong. Sorry! Support for multi-dimensional arrays was not added for the IntermediateSerializer.
As a workaround you can use a jagged array [][] instead of a multi-dimensional one [,]. Jagged arrays are actually faster than multi-dimensional ones too.

Your comment made me realize something, and it was totally an error on my end. I was using the wrong Ignore tag for a multidimentional array in one of my classes and it was tripping up on that. Was using [XmlIgnore] instead of [ContentSerializerIgnore]

Thank you so much for your time!

1 Like

I highly recommend System.Xml.Linq.

MSDN Page for Xml.Linq
Good Xml.Linq tutorial

Keep in mind you do not have to use Linq to access your Xml data. You can use XElement and XDocument
or any other type provided as normal without Linq.

For some reason I can only put in two hyperlinks so here is a stackoverflow question that shows how to use Xml.Linq for custom types.