Unable to deserialize XML file after changing object class

Viewed 112

I'm working on a level editor currently in XNA/Monogame and I'm running into an issue whenever I want to improve my "Level" class. Because my XML file is serialized as an object of my Level class, whenever I make changes to it I can no longer deserialize the file.

For example, here's my Level class:

public class Level {
    public int width;
    public int height;
    public Tile[] tiles;

    public Level(Tile[,] tileArray) {
        width = tileArray.GetLength(0);
        height = tileArray.GetLength(1);
        tiles = new Tile[width * height];
        int i = 0;
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                tiles[i] = tileArray[x, y];
                i++;
            }
        }
    }
    public Level() {} // to allow for serialization
}

Now, if I were to change one line, say, add a string "name," I would get errors saying that I am unable to cast certain types to each other or get an "end of stream" error message:

public class Level {
    public int width;
    public int height;
    public Tile[] tiles;
    public string name;
    
    //...
}
An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in MonoGame.Framework.dll
Content reader could not be found for System.String type.

Here's my XML Serializer and Deserializer functions:

public static void Save<T>(string path, T obj) {
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;

    using (XmlWriter writer = XmlWriter.Create(path, settings)) {
        IntermediateSerializer.Serialize(writer, obj, path);
    }
    Debug.Print(path);
}

// I use the XNA content manager to load my XML file as the <XNAContent> header is added to the file.

It almost seems to be random which error I get when I do this. Is there any way I can convert my XML files to be compatible with the updated classes?

0 Answers
Related