如何解决更改对象类后无法反序列化 XML 文件
我目前正在 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 举报,一经查实,本站将立刻删除。