I wanna xml serialize a custom object that contains other custom objects. I do it like below but it wont work.
class A()
{
public B b;
}
class B()
{
public int a;
}
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(A));
XmlAttributes xmlAttributes = new XmlAttributes();
System.IO.StreamWriter file = new System.IO.StreamWriter( @"d:\SerializationOverview.xml");
writer.Serialize(file, new A());
file.Close();
the result is:
<A>
</A>
Firstly, both
AandBneed to be markedpublic.The
new A()has not got a value inbto serialize; it doesn’t serialize nulls. Give it a value:Also: try to prefer properties over public fields.
For example: