I have a problem when using the XmlSerializer class for deserializing an xml stream.
I have an object like that :
public class Test
{
[XmlElement("data")]
public AnotherObject Data
{
get; set;
}
// other properties
}
It will work properly when I will try to deserialize that :
<Test>
<data>...</data>
<something else... />
</Test>
However, if the xml is like :
<Test>
<something else... />
</Test>
When I will try to read the Data property of the newly created object, the code will throw an exception. If I try to observe the object, the debugger prints a “Could not evaluate exception”.
I want the property to exist, even if there is no corresponding tag in the XML.
Is it possible to do that ?
Thanks !
XML deserialization uses the calls the default constructor before setting the properties. You could set the
dataproperty to a default value there that will be overwritten if one is loaded from the deserialization.