Here is my serialization function:

public static void SaveInXmlFormatt(List<Employee> objGraph, string fileName) { XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Employee>)); using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { xmlFormat.Serialize(fStream, objGraph); } Console.WriteLine("--> Сохранение объекта в XML-формат"); } 

At the same time, the Employee class is the base class for the Manager and Clerk classes. My task is to serialize a List(Employee) that contains its classes of successors ( Manager , Clerk ). When this function is called, it throws a System.InvalidOperationException .

    1 answer 1

    Try [XmlInclude] attribute from the Employee class:

     [XmlInclude(typeof(Manager)), XmlInclude(typeof(Clerk)), XmlType] public class Employee { ... 
    • It is possible, on the contrary, to teach the serialization method to look for successors by reflection. - Monk
    • @Monk: It just didn’t work for me, since the constructor was called all the same by the base type. But maybe it can be done through a proxy type. - VladD