如何在C#中从xml序列化/反序列化对象矩阵?

如何在C#中从xml序列化/反序列化对象矩阵?,c#,xml,matrix,serialization,deserialization,C#,Xml,Matrix,Serialization,Deserialization,我的任务是保存和装载扫雷艇板。我在保存董事会矩阵时遇到问题。这些是我的扫雷艇的财产: [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; } publ

我的任务是保存和装载扫雷艇板。我在保存董事会矩阵时遇到问题。这些是我的扫雷艇的财产:

[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; }
瓷砖属性:

[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++];
    }
}
感谢您的帮助

编辑:这是个例外

System.InvalidOperationException:'XML文档(7,4)中存在错误。 内部异常1:NullReferenceException:对象引用未设置为对象的实例

编辑2:保存代码

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内容


EDIT4:谢谢您的尝试,但这一切都不起作用,因此我将重写代码,使用列表而不是矩阵。

您可以尝试更改设置,如下所示:

set
{
    var height = value.Max(t=>t.Y);
    var width = value.Max(t=>t.X);
    Grid = new Tile[height, width];
    foreach(var tile in value)
    {
      Grid[tile.Y,tile.X]=tile;
    }
}
您可能不想使用游戏对象的高度和宽度属性,因为这样您就需要假定这些属性是在该属性之前设置的。不妨自己计算一下。当你在做这件事时,最好也改变get,然后扔掉Height/Width,或者改变它们以实际拉动网格的当前宽度/高度:

get
{
    var temp = new List<Tile>(Grid.Length);
    for (int i = 0; i < Grid.GetLength(0); i++)
        for (int j = 0; j < Grid.GetLength(1); j++)
            temp.Add(Grid[i, j]);
    return temp;
}
get
{
var temp=新列表(网格长度);
for(int i=0;i
反序列化属性ListFromMatrix时出现错误

public List<Tile> ListFromMatrix {get; set;}


// when it comes time to serialize
ListFromMatrix = CreateList();
using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
{
    XmlSerializer xs = new XmlSerializer(typeof(Game));
    xs.Serialize(fs, this.Game);
}

private List<Tile> CreateList()
{
    var 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;
}
public List ListFromMatrix{get;set;}
//当需要序列化时
ListFromMatrix=CreateList();
使用(FileStream fs=newfilestream(sfd.FileName,FileMode.Create))
{
XmlSerializer xs=新的XmlSerializer(typeof(Game));
序列化(fs,this.Game);
}
私有列表CreateList()
{
var temp=新列表(高度*宽度);
对于(int i=0;i
引发了什么异常?我已经用异常thrown编辑了这篇文章,我自己不会在这里使用数组[,]。特别是当需要序列化时。我会选择一个简单的列表,然后计算所需的索引,例如IndexOfTile=Game.Height*row+column。我正因为这个原因考虑重写所有内容,但我想要一个更优雅的解决方案。老手的建议是,先让它工作。我尝试了这个解决方案,即使是硬编码值也会引发同样的异常