有没有一种方法,以避免XmlSerializer的反序列化时不能初始化空的财产?初始化、财产、序列化、方法

2023-09-04 00:11:07 作者:我们是永不相交的平行線

我有这个类:

public class MySerializableClass
{
    public List<MyObject> MyList { get; set; }
}

如果MYLIST是当MySerializableClass序列化空,我需要把它空时,它的deserialised太多,但XmlSerializer的总是初始化它时,它deserialises我的课。

If MyList is null when MySerializableClass is serialized, I need to have it null when it's deserialised too, but the XmlSerializer always initializes it when it deserialises my class.

有没有办法来避免它初始化为空的属性?

Is there a way to avoid it initializing null properties?

MYLIST甚至没有记录在序列化的文件时,它的空。当我和空值加载它,并再次保存它,MYLIST不是空了,它的序列化作为一个名单,LT;> 0的物品,但不能为null

MyList is not even recorded in the serialized file when it's null. When I load it with null values, and save it again, MyList is not null anymore, it's serialized as a List<> with 0 items, but not null.

感谢。

更多信息:

这是IsDeserializing财产是不可行的,由于在类的构造一些code限制

An IsDeserializing property is not viable due to some code restrictions in the structure of the class

推荐答案

这看起来像一个错误...

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<MyObject> MyList { get; set; }

它系列化MYLIST属性如下:

It serializes the MyList property as follows :

<MyList xsi:nil="true" />

所以XML清楚地表明,该列表是空的,但反序列化后,它仍然初始化为空列表...

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

如果您更换名单,其中,为MyObject&GT; 为MyObject [] ,它工作正常(即使没有 ISNULLABLE = TRUE ),但它可能不是你想要的......

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

您或许应该报告这一对连接。

You should probably report this on Connect.

 
精彩推荐
图片推荐