链表无法使用 XMLSerializer 进行序列化
LinkedList 无法使用 XmlSerializer 进行序列化。
现在,如何从序列化对象 LinkedList 保存/检索数据。我应该实现自定义序列化吗?
我尝试做什么:
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); }
}
}
我获得了什么(添加姓名、年龄和 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>
所以,链接列表中的项目尚未序列化...:(
A LinkedList can't be serialized using XmlSerializer.
Now, how to however save/retrieve data from a serialized objet LinkedList. Should I implement custom serialization?
What I tried to do:
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); }
}
}
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... :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种可能性是创建一个可序列化的集合,其中包含与链接列表相同的对象。例如(未经测试):
然后序列化
list
。如果 Foo 是引用类型,并且您不关心插入/删除现在更昂贵的事实,因为您只使用List
保存对数据的引用以用于序列化和反序列化目的。当您将其从序列化流中拉出时,您只需将其返回到LinkedList
即可获得您最初使用链表的所有优势。这是我刚刚制作来演示的一个小控制台应用程序。我在 VS2008 中做到了这一点,但我认为我没有使用任何令人惊讶的东西——只是使用数组初始化语法来节省一些垂直空间。
示例代码:
和主要应用程序代码:
这会将以下内容踢出到控制台:
One possibility would be the creation of a serializable collection that contains the same objects as the linked list. E.g. (untested):
Then serialize
list
instead. This isn't going to create a lot of overhead ifFoo
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 theList<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 aLinkedList<T>
to get all those advantages you were using a linked list for in the first place.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.
Sample Code:
And the main application code:
This kicks the following out to the console: