3

I can serialize a List if the list is not null.

But if this list is null, I get an empty list on deserialization.

What do I have to do in order to get null on deserialization, when the list was null on serialization?

Here are my attempts so far:

        [System.Xml.Serialization.XmlArray("WorksButIsNoList", IsNullable = true)]
    public MyClass[] WorksButIsNoList { get; set; }

    [System.Xml.Serialization.XmlArray("ThrowsException", IsNullable = true)]
    public List<MyClass> ThrowsException { get; set; }

    [System.Xml.Serialization.XmlElement("IsntNull", IsNullable = true)]
    public List<MyClass> IsntNull { get; set; }

    [System.Xml.Serialization.XmlElement("ThrowsException2", IsNullable = true)]
    public List<int> ThrowsException2 { get; set; }

    [System.Xml.Serialization.XmlElement("IsntNull2", IsNullable = true)]
    public List<int?> IsntNull2 { get; set; }
MTR
  • 1,690
  • 3
  • 20
  • 47
  • XmlSerializer really isn't very interested in null lists... when deserializing, if it doesn't have data, it *won't touch the value*. Frankly, I would change your code such that you don't need to distinguish between a `null` list and an *empty* list. – Marc Gravell Feb 07 '13 at 10:56
  • 1
    duplicate. http://stackoverflow.com/questions/2188619/is-there-a-way-to-avoid-the-xmlserializer-to-not-initialize-a-null-property-when – Szymon Rozga Feb 07 '13 at 12:56
  • During deserialization null lists are deserialized as empty lists. – bubi Nov 13 '20 at 18:30

0 Answers0