I’m currently working on serializing in C# for the first time and I’m using the XmlSerializer class, but I’ve come across a problem. I have two classes which refer eachother, and this causes an error when serializing.
The project is a program for class diagram drawing. Each class has a list of the arrows pointing to or from it:
public List<UMLArrow> arrowlist;
And every arrow has a starting and an ending class:
public UMLClass startingClass;
public UMLClass endClass;
I figured that when I try to serialize this, I get into an infinite loop because of this problem:
<UMLClass>
<List<UMLArrow> >
<UMLClass>
<List<UMLArrow> >
...
<List<UMLArrow> >
</UMLClass>
</List<UMLArrow>>
<UMLClass>
Does anyone know a solution to this?
The
XmlSerializerdoes not support circular references. You need to use a serializer which does. TheDataContractSerializer(if initialized withpreserveObjectReferences = true) or theNetDataContractSerializerdo support it, so you should consider changing the serializer you use.