本文介绍了解串名单,其中,INT>与XmlSerializer的导致额外的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,这与XmlSerializer的和通用的清单一个奇怪的行为(尤其是名单,其中,INT> )。我想知道是否有人之前已经看到了这一点,或知道发生了什么事。它看起来好像系列化工作正常,但反序列化要额外的项目添加到列表中。下面的code说明了问题。

I'm noticing an odd behavior with the XmlSerializer and generic lists (specifically List<int>). I was wondering if anyone has seen this before or knows what's going on. It appears as though the serialization works fine but the deserialization wants to add extra items to the list. The code below demonstrates the problem.

序列化类:

public class ListTest
{
    public int[] Array { get; set; }
    public List<int> List { get; set; }

    public ListTest()
    {
        Array = new[] {1, 2, 3, 4};
        List = new List<int>(Array);
    }
}

测试code:

Test code:

ListTest listTest = new ListTest();
Debug.WriteLine("Initial Array: {0}", (object)String.Join(", ", listTest.Array));
Debug.WriteLine("Initial List: {0}", (object)String.Join(", ", listTest.List));

XmlSerializer serializer = new XmlSerializer(typeof(ListTest));
StringBuilder xml = new StringBuilder();
using(TextWriter writer = new StringWriter(xml))
{
    serializer.Serialize(writer, listTest);
}

Debug.WriteLine("XML: {0}", (object)xml.ToString());

using(TextReader reader = new StringReader(xml.ToString()))
{
    listTest = (ListTest) serializer.Deserialize(reader);
}

Debug.WriteLine("Deserialized Array: {0}", (object)String.Join(", ", listTest.Array));
Debug.WriteLine("Deserialized List: {0}", (object)String.Join(", ", listTest.List));

调试输出:

Initial Array: 1, 2, 3, 4
Initial List: 1, 2, 3, 4
XML: <?xml version="1.0" encoding="utf-16"?>
<ListTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Array>
    <int>1</int>
    <int>2</int>
    <int>3</int>
    <int>4</int>
  </Array>
  <List>
    <int>1</int>
    <int>2</int>
    <int>3</int>
    <int>4</int>
  </List>
</ListTest>
Deserialized Array: 1, 2, 3, 4
Deserialized List: 1, 2, 3, 4, 1, 2, 3, 4

请注意,无论是数组和列表似乎已经连载到XML正常,但在反序列化的阵列出来正确的,但名单回来有一套相同的项目。任何想法?

Notice that both the array and list appear to have serialized to XML correctly but on deserialization the array comes out correct but the list comes back with a duplicate set of items. Any ideas?

推荐答案

这是因为你被初始化在构造函数列表。当你去反序列化,一个新的ListTest创建,然后填充从状态的对象。

It happens because you are initializing the List in the constructor. When you go to deserialize, a new ListTest is created and then it populate the object from state.

这样想的工作流程

  1. 创建一个新的ListTest
  2. 执行构造函数(加1,2,3,4)
  3. 反序列化的XML状态,并添加1,2,3,4到List

有一个简单的解决办法是初始化对象的构造函数的范围之内。

A simple solution would be to init the object outside the scope of the constructor.

public class ListTest
{
    public int[] Array { get; set; }
    public List<int> List { get; set; }

    public ListTest()
    {

    }

    public void Init() 
    {
        Array = new[] { 1, 2, 3, 4 };
        List = new List<int>(Array);
    }
}

ListTest listTest = new ListTest();
listTest.Init(); //manually call this to do the initial seed

这篇关于解串名单,其中,INT&GT;与XmlSerializer的导致额外的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:53