Home All Groups Group Topic Archive Search About

How to deserialize null collections

Author
16 Nov 2004 3:58 PM
Ryan Gilchrist
Hi,

There appears to be a difference in how null arrays and collections
are deserialized using XmlSerializer.  A null array is serialized into
nothing, then deserialized back to null again (which one could argue
is the correct behaviour), while a collection is serialized into
nothing (like the array) but then deserialized into a collection
instance with 0 elements.  Obviously, you'd have trouble telling the
difference between a null collection and an empty one after
serilization.

For example, the class below would deserialize to an instance with
MyArray = null and MyCollection = StringCollection with 0 elements
(the data members could just as well be properties).

public class Foo
{
   public string[] MyArray = null;
   public StringCollection MyCollection = null;
}

Is there a solution or at least a workaround to this problem?

Cheers,
Ryan

AddThis Social Bookmark Button