I have a code like this:
[Serializable]
public class A {
public int X { get; set; }
}
[Serializable]
public class B : A{
}
[Serializable]
public class C {
public A A { get; set; }
}
...
public string Serialize<T>(T obj)
{
StringBuilder stringBuilder = new StringBuilder();
TextWriter stringWriter = new StringWriter(stringBuilder);
XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(xmlWriter, obj);
xmlWriter.Close();
stringWriter.Close();
return stringBuilder.ToString();
}
private void Run() {
C c = new C() {A = new B()};
string str = Serialize(c);
}
I have System.InvalidOperationException
in execute of string str = SerializationManager.Serialize(c);
with text "There was an error generating the XML document."
How I must write my code to serialize c
without exception?