编程语言
首页 > 编程语言> > C#Xml Serializer将列表反序列化为0而不是null

C#Xml Serializer将列表反序列化为0而不是null

作者:互联网

我很困惑XmlSerializer在幕后如何工作.我有一个类将XML反序列化为一个对象.我所看到的是以下两个元素不是被反序列化的Xml的一部分.

[XmlRootAttribute("MyClass", Namespace = "", IsNullable = false)]
public class MyClass
{
    private string comments;
    public string Comments
    {
        set { comments = value; }
        get { return comments; }
    }

    private System.Collections.Generic.List<string> tests = null;
    public System.Collections.Generic.List<string> Tests
    {
        get { return tests; }
        set { tests = value; }
    }
}

我们以下面的XML为例:

<MyClass>
  <SomeNode>value</SomeNode>
</MyClass>

您注意到测试和注释不是XML的一部分.

当此XML被反序列化时,Comments为null(这是预期的),而Tests是一个计数为0的空列表.

如果有人能向我解释这一点,我将不胜感激.我更喜欢的是,如果< Tests> XML中缺少该列表,但列表应保持为null,但如果(可能为空)节点< Tests />如果存在,那么列表应该被分配.

解决方法:

你所观察到的是那些引用可修改集合的成员,例如List< T>.在反序列化开始时由XmlSerializer自动预分配.我不知道有任何记录此行为的地方.它可能与this answerXML Deserialization of collection property with code defaults中描述的行为有关,这解释了,从XmlSerializer supports adding to get-only and pre-allocated collections开始,如果预先分配的集合包含默认项,则反序列化的项将被附加到它 – 可能复制内容.微软可能只是选择在反序列化开始时预先分配所有可修改的集合,这是实现它的最简单方法.

该答案的解决方法,即使用代理数组属性,也适用于此.由于无法附加数组,因此XmlSerializer必须累积所有值,并在反序列化完成时将其设置回来.但是如果从未遇到相关标记,则XmlSerializer显然不会开始累积值,因此不会调用数组setter.这似乎阻止了您不想要的集合的默认预分配:

[XmlRootAttribute("MyClass", Namespace = "", IsNullable = false)]
public class MyClass
{
    private string comments;
    public string Comments
    {
        set { comments = value; }
        get { return comments; }
    }

    private System.Collections.Generic.List<string> tests = null;

    [XmlIgnore]
    public System.Collections.Generic.List<string> Tests
    {
        get { return tests; }
        set { tests = value; }
    }

    [XmlArray("Tests")]
    public string[] TestsArray
    {
        get
        {
            return (Tests == null ? null : Tests.ToArray());
        }
        set
        {
            if (value == null)
                return;
            (Tests = Tests ?? new List<string>(value.Length)).AddRange(value);
        }
    }
}

示例.Net fiddle显示仅在适当时分配测试.

标签:c,serialization,xml,xmlserializer
来源: https://codeday.me/bug/20190701/1349446.html