链表无法使用 XMLSerializer 进行序列化

发布于 2024-08-21 13:20:15 字数 1512 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-08-28 13:20:15

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

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

然后序列化 list 。如果 Foo 是引用类型,并且您不关心插入/删除现在更昂贵的事实,因为您只使用List 保存对数据的引用以用于序列化和反序列化目的。当您将其从序列化流中拉出时,您只需将其返回到 LinkedList 即可获得您最初使用链表的所有优势。


这是我刚刚制作来演示的一个小控制台应用程序。我在 VS2008 中做到了这一点,但我认为我没有使用任何令人惊讶的东西——只是使用数组初始化语法来节省一些垂直空间。

示例代码:

[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); }
    }
}

和主要应用程序代码:

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>

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);

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.


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:

[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); }
    }
}

And the main application 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;
    }
}

This kicks the following out to the console:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文