问题描述
我目前正在 XNA/Monogame 中开发关卡编辑器,每当我想改进“关卡”课程时都会遇到问题。因为我的 XML 文件被序列化为我的 Level 类的对象,所以每当我对其进行更改时,我都无法再反序列化该文件。
例如,这是我的 Level 课程:
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
}
现在,如果我要更改一行,例如添加一个字符串“name”,我会收到错误消息,指出我无法将某些类型相互转换或收到“流结束”错误消息:
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.
这是我的 XML Serializer 和 Deserializer 函数:
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.
当我这样做时,我得到的错误几乎似乎是随机的。有什么方法可以将我的 XML 文件转换为与更新的类兼容?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)