Is there a way to avoid the XmlSerializer to not initialize a null property when deserializing?

后端 未结 4 1745
旧巷少年郎
旧巷少年郎 2020-12-16 18:00

I have this class:

public class MySerializableClass
{
    public List MyList { get; set; }
}

If MyList is null when MySeria

4条回答
  •  一向
    一向 (楼主)
    2020-12-16 18:51

    This looks like a bug...

    Even if you try to mark the property as nullable, it doesn't seem to work.

    [XmlArray(IsNullable = true)]
    public List MyList { get; set; }
    

    It serializes the MyList property as follows :

    
    

    So the XML clearly indicates that the list is null, but after deserialization, it is still initialized to an empty list...

    If you replace List with MyObject[], it works fine (even without IsNullable = true), but it's probably not what you want...

    You should probably report this on Connect.

提交回复
热议问题