0

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

3
  • 1
    "my data model which I can post here if you like" - we like... Commented Nov 19, 2013 at 21:22
  • XMLSerializer does not play well with circular references. You might need to use SoapFormatter instead. Commented Nov 19, 2013 at 21:24
  • @DStanley Thanks, I have posted the data model :) Commented Nov 19, 2013 at 21:26

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I have already used BinaryFormatter to serialize, and it seems to have worked. However i will also look into this solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.