【发布时间】:2020-03-10 11:53:48
【问题描述】:
我有一个任务是保存和加载扫雷板。我在保存电路板矩阵时遇到问题。这些是我的扫雷属性:
[XmlIgnore]
public Tile[,] Grid { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int NumberOfMines { get; set; }
平铺属性:
public int X { get; set; }
public int Y { get; set; }
public int NeighbourMines { get; set; }
public bool IsMine { get; set; }
public bool IsRevealed { get; set; }
public bool IsFlagged { get; set; }
我尝试过这样的事情:
public List<Tile> ListFromMatrix
{
get
{
List<Tile> temp = new List<Tile>(Height * Width);
for (int i = 0; i < Height; i++)
for (int j = 0; j < Width; j++)
temp.Add(Grid[i, j]);
return temp;
}
set
{
//Grid = new Tile[Height, Width];
int it = 0;
for (int i = 0; i < Height; i++)
for (int j = 0; j < Width; j++)
Grid[i, j] = value[it++];
}
}
保存到文件中可以正常工作,但从中加载会导致异常。而且我真的不知道如何调试它,因为异常被抛出:
//this.Game is the Minesweeper reference in the main form
this.Game = (Game)xs.Deserialize(fileStream);
感谢任何帮助!
编辑:这是个例外
System.InvalidOperationException: 'XML 文档中存在错误 (7, 4)。 内部异常 1:NullReferenceException:对象引用未设置为对象的实例。
EDIT2:保存代码
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
{
XmlSerializer xs = new XmlSerializer(typeof(Game));
xs.Serialize(fs, this.Game);
}
}
EDIT3:这里是xml内容https://pastebin.com/0vkxQC5A
EDIT4:感谢您的尝试,但没有任何效果,所以我将重写代码以使用列表而不是矩阵。
【问题讨论】:
-
抛出了什么异常?
-
我已经编辑了帖子,但抛出了异常
-
我自己不会在这里使用数组[,]。特别是在需要序列化的时候。我会选择一个简单的 List
然后计算所需的索引,例如 IndexOfTile = Game.Height * row + column。 -
我正考虑重写所有内容,但我想要一个更优雅的解决方案。
-
老手的小费,先搞定吧。
标签: c# xml matrix serialization deserialization