user2602079
user2602079

Reputation: 1393

Error while XML serializing an object

I thought my class conformed to all the XML serialization requirements. Here is part of the error message.

InnerException: System.InvalidOperationException HResult=-2146233079 Message=A circular reference was detected while serializing an object of type TheseusAndTheMinotaur.LevelDesigner.ModelMap. Source=System.Xml StackTrace: 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) at 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) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterModelMap.Write5_ModelMap(Object o) InnerException:

Is this the error?: Message=A circular reference was detected while serializing an object of type TheseusAndTheMinotaur.LevelDesigner.ModelMap. In my data model which I can post here if you like, I have a map that has a list of cells, and each cell knows "myMap" of type Map. Is this causing some circular error (looping)?

I have not added any attributes to my code, as I am aware if it conforms to the XML serialization requirements i do not need to?

Data Model:

    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;
            }
        }
    }
}

Cell Class:

    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; }
    }
}

My serialisation code:

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);
    }

Thanks

Upvotes: 0

Views: 1935

Answers (1)

Matthew Steven Monkan
Matthew Steven Monkan

Reputation: 9130

Looks like you have a circular reference like this:

ModelMap.myCells -> (traverse List<Cell>) -> Cell.myMap -> ModelMap

where both ModelMaps refer to the same instance, so serializing would loop forever.

This answer explains your situation well, and explains a couple of solutions.

If you do a Ctrl-F on "circular" on this article, you can see a common XML structure for supporting repeated object instances using id and ref attributes on XML elements; this is crucial for deserialization of the file back into the same object graph.

Upvotes: 3

Related Questions