I have a super class and two subclasses and I want to serialize the objects of the subclasses as a list and deserialize it
I tried to use a super class list which had objects from both subclasses but ended up in an exception.
Is there any way to do it?
Type1 t = new Type1() { text="123" ,opt1=true,opt2=true};
Type2 t1 = new Type2() { text="1234",isAnswer=false};
Question q1 = new Question() { text="12321"};
Question q2 = new Question() { text = "12321" };
List<Question> q = new List<Question>() { t1 };
FileStream fs = new FileStream("aa.xml", FileMode.OpenOrCreate, FileAccess.Write);
XmlSerializer xs = new XmlSerializer(typeof(List<Question>));
//Exception is generated here InvalidOperationException
//there was error genearating the XML document
xs.Serialize(fs, q);
fs.Close();
Try passing the types to be known to the serializer, such as,
serializer = new XmlSerializer(typeof(T), extraTypes);where extraTypes is an array of types that you have to be serialized.