本文介绍了链表< T>不能使用XmlSerializer进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个LinkedList的<一个href="https://connect.microsoft.com/VisualStudio/feedback/details/93538/linkedlist-t-can-not-be-serialized-using-the-xmlserializer?wa=wsignin1.0"相对=nofollow>不能序列使用XmlSerializer的。

A LinkedList can't be serialized using XmlSerializer.

现在,如何然而保存/从一个序列化的客体LinkedList的检索数据。我应该实现自定义序列化?

Now, how to however save/retrieve data from a serialized objet LinkedList. Should I implement custom serialization?

我试图做的:

using System.Xml.Serialization;

[Serializable()]
public class TestClass
{
    private int _Id;
    private string _Name;
    private int _Age;
    private LinkedList<int> _linkedList = new LinkedList<int>();

    public string Name {
        get { return _Name; }
        set { _Name = value; }
    }
    
    public string Age {
        get { return _Age; }
        set { _Age = value; }
    }
    
    [XmlArray()]
    public List<int> MyLinkedList {
        get { return new List<int>(_linkedList); }
        set { _linkedList = new LinkedList<int>(value); }
    }
}

我获得什么(addind姓名,年龄和mylinkedlist一些项目):

What I've obtained(addind name, age and some items in the mylinkedlist):

<?xml version="1.0"?>
<TestClass 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>testName</Name>
  <Age>10</Age>
  <MyLinkedList />
</TestClass>

因此​​,在该链接的表中的项目还没有被序列...:(

So, the items in the linked list hadn't been serialized... :(

推荐答案

一种可能性是创建一个序列化的集合,其中包含了相同的对象链表。例如。 (未经测试):

One possibility would be the creation of a serializable collection that contains the same objects as the linked list. E.g. (untested):

LinkedList<Foo> ll = PopulateLinkedList();
List<Foo> list = new List<Foo>(ll);

然后连载列表代替。这不是要创造大量的开销,如果是一个引用类型,你不关心的事实,插入/缺失,现在更贵的B / c您现在只用到了名单,其中,T&GT; 持有引用的数据序列化和反序列化的目的。当你拉出来的序列化流的,你可以把它恢复成的LinkedList&LT; T&GT; 来让所有那些你使用的是链表的优势在第一的地方。

Then serialize list instead. This isn't going to create a lot of overhead if Foo is a reference type, and you don't care about the fact that insertions/deletions are now more expensive b/c you're only using the List<T> to hold references to the data for serialization and deserialization purposes. When you pull it out of the serialized stream, you can just turn it back into a LinkedList<T> to get all those advantages you were using a linked list for in the first place.

这里有一个小控制台应用程序我刚刚刮起来证明。我做了它在VS2008,但我不认为我用什么surprising--只是数组初始化语法来节省垂直空间。

Here's a little console app I just whipped up to demonstrate. I did it in VS2008, but I don't think I used anything surprising-- just the array initialization syntax to save some vertical space.

样品code:

[Serializable]
public class MyClass
{
    private string name;
    private int age;
    private LinkedList<int> linkedList = new LinkedList<int>();

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    [XmlArray]
    public List<int> MyLinkedList
    {
        get { return new List<int>(linkedList); }
        set { linkedList = new LinkedList<int>(value); }
    }
}

和主要应用code:

class Program
{
    static void Main(string[] args)
    {
        MyClass c = new MyClass();

        c.Age = 42;
        c.Name = "MyName";
        c.MyLinkedList = new List<int>() { 1, 4, 9 }; // Your property impl requires this be set all at once, not one element at a time via the property.

        XmlSerializer s = new XmlSerializer(typeof(MyClass));
        StringWriter outStream = new StringWriter();
        s.Serialize(outStream, c);

        Console.Write(outStream.ToString());

        return;
    }
}

这踢下到控制台:

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>MyName</Name>
  <Age>42</Age>
  <MyLinkedList>
    <int>1</int>
    <int>4</int>
    <int>9</int>
  </MyLinkedList>
</MyClass>

这篇关于链表&LT; T&GT;不能使用XmlSerializer进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 01:48