XML序列化对象时出错

时间:2013-11-19 21:21:07

标签: c# xml-serialization

我认为我的课程符合所有XML序列化要求。以下是错误消息的一部分。

InnerException:System.InvalidOperationException        的HResult = -2146233079        Message =在序列化TheseusAndTheMinotaur.LevelDesigner.ModelMap类型的对象时检测到循环引用。        来源=的System.Xml        堆栈跟踪:             at System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name,String ns,Object o,Boolean writePrefixed,XmlSerializerNamespaces xmlns)             at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterModelMap.Write4_ModelMap(String n,String ns,ModelMap o,Boolean isNullable,Boolean needType)             在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterModelMap.Write3_Cell(String n,String ns,Cell o,Boolean isNullable,Boolean needType)             at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterModelMap.Write4_ModelMap(String n,String ns,ModelMap o,Boolean isNullable,Boolean needType)             在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterModelMap.Write5_ModelMap(Object o)        InnerException:

这是错误吗?:Message =在序列化TheseusAndTheMinotaur.LevelDesigner.ModelMap类型的对象时检测到循环引用。在我可以发布的数据模型中,如果你愿意,我有一个包含单元格列表的地图,每个单元格都知道Map类型的“myMap”。这会导致一些循环错误(循环)吗?

我没有在代码中添加任何属性,因为我知道它是否符合我不需要的XML序列化要求?

数据模型:

    public class ModelMap
    {
        public ModelMap()
        {
        }

        public int rows { get; set; }
        public int cols { get; set; }
        public int boardXPos { get; set; }
        public int boardYPos { get; set; }
        public int myCellSize { get; set; }
        public string HorizontalWallSource { get; set; }
        public string VerticalWallSource { get; set; }
        public string cellBgSource { get; set; }
        public string theseusSource { get; set; }
        public string minotaurSource { get; set; }
        public string exitSource { get; set; }
        public int myWidth { get; set; }
        public int myHeight { get; set; }
        private List<Cell> m_cells = new List<Cell>();
        public List<Cell> myCells
        {
            get
            {
                return m_cells;
            }
            set
            {
                m_cells = value;
            }
        }
    }
}

细胞分类:

    public class Cell
    {
        public Cell()
        {
        }

        public ModelMap myMap { get; set; }
        public bool hasMinotaur { get; set; }
        public bool hasTheseus { get; set; }
        public bool isExit { get; set; }
        public int mySize { get; set; }
        public CellSide myRightWall { get; set; }
        public CellSide myBottomWall { get; set; }
        public int myColumn { get; set; }
        public int myRow { get; set; }
    }

    public class CellSide
    {
        public CellSide()
        {
            this.hasWall = 0;
            this.isHighlighted = false;
        }

        public bool isHighlighted { get; set; }
        public int hasWall { get; set; }
    }
}

我的序列化代码:

private void btnSaveMap_Click(object sender, RoutedEventArgs e)
{
    XmlSerializer mySerializer = new
    XmlSerializer(typeof(ModelMap));

    ModelMap map = this.myMapController.myMap;

    using (var writer = new StreamWriter(@"c:\xml\test.xml"))
    {
        mySerializer.Serialize(writer, map);
    }

由于

1 个答案:

答案 0 :(得分:3)

看起来你有这样的循环引用:

ModelMap.myCells - &gt; (遍历List<Cell>) - &gt; Cell.myMap - &gt; ModelMap

其中两个ModelMap引用相同的实例,因此序列化将永远循环。

这个answer很好地解释了你的情况,并解释了几个解决方案。

如果在此article上对“循环”执行Ctrl-F,则可以看到使用XML元素上的idref属性支持重复对象实例的通用XML结构;这对于将文件反序列化回同一对象图是至关重要的。