有没有一种方法,以避免XmlSerializer的反序列化时不能初始化空的财产? [英] Is there a way to avoid the XmlSerializer to not initialize a null property when deserializing?

查看:211
本文介绍了有没有一种方法,以避免XmlSerializer的反序列化时不能初始化空的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类:

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.

这篇关于有没有一种方法,以避免XmlSerializer的反序列化时不能初始化空的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆