My repository has List<Student>
, List<Course>
and List<Enrolment>
where an Enrolment has Enrolment.Student and Enrolment.Course which are references one of the students or courses in the two previous lists.
When I use XmlSerializer on my repository it outputs redundant data as it serializes all properties of each student in List<Student>
then again for every reference to those same students in List<Enrolment>
. I'm looking for an elegant way to solve this.
After deserialization I can fix the references using the ID values in the duplicate object instances created by the deserialization but this seems hackish.
One method to fix the redundant output is to XmlIgnore Enrolment.Student and Enrolment.Course and create two more properties for serialization - Enrolment.StudentID and Enrolment.CourseID. However during deserialization, the references for Enrolment.Student and Enrolment.Course cannot be set (AFAIK) since the results of deserialization of List<Student>
and List<Course>
are not available.
Another method I thought of is to serialize lower down in my object hierarchy doing each of my Lists separately and controlling the order of deserialization - I rather not do this.
Another method would be to XmlIgnore List<Enrolment>
and create an enrolment serialization helper class that initializes List<Enrolment>
after the deserialization of itself is complete. This seems like a lot of effort.
How do other people serialize/deserialize multiple references to the same object using XmlSerializer?