XmlSerializer的和List< T>具有默认值的 [英] XmlSerializer and List<T> with default values

查看:108
本文介绍了XmlSerializer的和List< T>具有默认值的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我序列化和比反序列化是有类型的成员的类时,观察到一个怪异的行为名单,其中,T> 这是充满在施工时的默认值。不像基于阵列的属性类型的属性名单,其中,T> 不会得到在反序列化由XmlSerializer的清空

I observed a weird behavior when serializing and than deserializing a class that is having a member of type List<T> which was filled with default values at construction time. Unlike the array based property the property of type List<T> won't get emptied at deserialization by the XmlSerializer.

下面是我的code:

public class Program
{
    public class Config
    {
        public Config()
        {
            Test1 = new List<string>()  {"A", "B"};
            Test2 = new String[] {"A", "B"};
        }
        public List<string> Test1 {get;set;}
        public string[] Test2 {get;set;}
    }

    public static void Main()
    {
        XmlSerializer xmlSerializer =
            new XmlSerializer(typeof(Config));
        using(Stream s = new MemoryStream())
        {
            xmlSerializer.Serialize(s, new Config());
            s.Position = 0;
            xmlSerializer.Serialize(Console.Out,
                xmlSerializer.Deserialize(s));
        }
    }
}

这是输出:

<?xml version="1.0" encoding="ibm850"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Test1>
    <string>A</string>
    <string>B</string>
    <string>A</string>
    <string>B</string>
  </Test1>
  <Test2>
    <string>A</string>
    <string>B</string>
  </Test2>
</Config>

为什么名单,其中,T&GT; 一样的处理方法的XmlSerializer比数组和我能做些什么来改变这种行为

Why is the List<T> handled differently by XmlSerializer than the array and what can I do to change this behavior?

推荐答案

有趣的;我从来没有注意到,在过去,但它肯定是重复的。由于的XmlSerializer 不支持序列回调(以帮助您知道它是运行序列化),这是很难影响;可以说是最简单的答案是不把默认数据为在构造函数中的对象(虽然也许提供了一个工厂方法,做的)。

Interesting; I've never noticed that in the past, but it is definitely reproducible. Since XmlSerializer doesn't support serialization callbacks (to help you know that it is running for serialization) this is hard to influence; arguably the simplest answer is "don't put default data into the objects in the constructor" (although maybe offer a factory method that does that).

您的可以的尝试实施的IXmlSerializable ,但过于很难得到正确,即使是一个简单的例子。

You could try implementing IXmlSerializable, but that is overly hard to get right, even for a simple example.

我已经检查了,虽然和的DataContractSerializer 的行为这种方式 - 所以你也许可以切换到的DataContractSerializer ;这里是我的测试code与DCS:

I have checked, though, and DataContractSerializer does not behave this way - so you could perhaps switch to DataContractSerializer; here's my test code with DCS:

DataContractSerializer ser =
    new DataContractSerializer(typeof(Config));
using (Stream s = new MemoryStream())
{
    ser.WriteObject(s, new Config());
    s.Position = 0;
    using(var writer = XmlWriter.Create(Console.Out)) {
        ser.WriteObject(writer, ser.ReadObject(s));
    }
}

和这里是我的意思是由一个工厂方法:

and here is what I mean by a the factory method:

public class Config
{
    public Config()
    {
        Test1 = new List<string>();
        Test2 = nix;
    }
    public List<string> Test1 { get; set; }
    public string[] Test2 { get; set; }

    private static readonly string[] nix = new string[0];
    public static Config CreateDefault()
    {
        Config config = new Config();
        config.Test1.Add("A");
        config.Test1.Add("B");
        config.Test2 = new string[2] { "A", "B" };
        return config;
    }
}

这篇关于XmlSerializer的和List&LT; T&GT;具有默认值的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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