Reputation: 689
what is the best way to serialize with XmlSerialize classes with cross reference?
In the example below multiple B classes can point to the same A object. How do I serialize C without writing multiple A in the xml? My classes are big so I prefer using automatic serialization and not writing a complete
public class A{
int id;
}
public class B{
int id;
A a;
}
public class Root{
B[] bArray;
}
Upvotes: 0
Views: 235
Reputation: 1283
XmlSerializer cannot handle cross references. Try using DataContractSerializer with PreserveObjectReferences = true.
Upvotes: 1
Reputation: 34421
Should not make any difference that the classes are looped.
[XmlRoot("A")]
public class A
{
[XmlElement("id")]
int id;
}
[XmlRoot("B")]
public class B
{
[XmlElement("id")]
int id;
[XmlElement("A")]
A a;
}
[XmlRoot("Root")]
public class Root
{
[XmlElement("B")]
List<B> bArray;
}
Upvotes: 0