When deserialize XML into object, the object’s property becomes an empty list even if the property is absence from the XML, the expected behavior is the property is null.
For example, given a class
public class MyFoo
{
public string Id { get; set; }
[XmlArrayItem(“Name”)]
public List<string> Names { get; set; }
public override string ToString()
{
// output the serialized xml
return Serialization<MyFoo>.ObjectToXml(this, false, false, true);
}
}
given the serialized XML
<MyFoo>
<Id>1</Id>
</MyFoo>
As you see, only property “Id” has been populated before the serialization. In theory, if we deserialize this XML back to the object, we should have “Id” populated and “Names” would be null. In fact, after deserialization, the property “Names” is initialized as a List contains empty elements. as below
<MyFoo>
<Id>1</Id>
<Names />
</MyFoo>
Why it behaves like this?