如何序列化/反序列化空List<>使用XmlSerializer

时间:2013-02-07 10:53:27

标签: c# serialization xml-serialization

如果列表不为null,我可以序列化List。

但是如果这个列表为null,我会在反序列化时获得一个空列表。

当序列化时列表为空时,为了在反序列化时获取null,我该怎么做?

到目前为止,我的尝试是:

        [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; }

0 个答案:

没有答案