如何使用 Xml 序列化 LinkedList?

发布于 2024-08-21 15:46:55 字数 1935 浏览 2 评论 0 原文

.NET 2

实际上,有一些方法可以 XML 序列化 List。 如果我有一个具有公共 LinkedList 成员的对象怎么办?

无需从 LinkedList 创建公共副本作为 List。也许是像二进制一样控制 Xml 序列化的方法(OnSerializing、OnDeserializing)。

无法使用公共 LinkedList 成员对对象进行 XML 序列化?

编辑:

使用 IXmlSerialized 修复的测试示例

using System.Xml.Serialization;

public class Foo : IXmlSerializable {        
    private int _Id;
    private string _Name;

    public string Name {
        get { return _Name; }
        set { _Name = value; }
    }        
    
    private int _Age;
    public string Age {
        get { return _Age; }
        set { _Age = value; }
    }
    
    private LinkedList<Bar> _linkedList = new LinkedList<Bar>();
    [XmlArray()]
    public List<Bar> MyLinkedList {
        get { return _linkedList; }
        set { _linkedList = value; }
    }
    
    public System.Xml.Schema.XmlSchema GetSchema() {
        return null;
    }
    
    public void ReadXml(System.Xml.XmlReader reader) {
        _Name = reader.ReadString(); // ? '            
        _Age = reader.ReadString(); // ? '
        // foreach MyLinkedList 
    }
    
    public void WriteXml(System.Xml.XmlWriter writer) {
        writer.WriteString(_Name); // ? '
        writer.WriteString(_Age); // ? '
        // foreach MyLinkedList 
    }
}

.NET 2

Actually there are methods to XML serialize a List<T>.
What if I have an object that has a public LinkedList<T> member?

Without creating a public duplicate as List<T> from LinkedList<T>. Maybe a way to control the Xml serialization like binary (OnSerializing, OnDeserializing).

Will be impossible to XML serialize a object with a public LinkedList<T> member?

EDIT:

An test example to fix with IXmlSerializable

using System.Xml.Serialization;

public class Foo : IXmlSerializable {        
    private int _Id;
    private string _Name;

    public string Name {
        get { return _Name; }
        set { _Name = value; }
    }        
    
    private int _Age;
    public string Age {
        get { return _Age; }
        set { _Age = value; }
    }
    
    private LinkedList<Bar> _linkedList = new LinkedList<Bar>();
    [XmlArray()]
    public List<Bar> MyLinkedList {
        get { return _linkedList; }
        set { _linkedList = value; }
    }
    
    public System.Xml.Schema.XmlSchema GetSchema() {
        return null;
    }
    
    public void ReadXml(System.Xml.XmlReader reader) {
        _Name = reader.ReadString(); // ? '            
        _Age = reader.ReadString(); // ? '
        // foreach MyLinkedList 
    }
    
    public void WriteXml(System.Xml.XmlWriter writer) {
        writer.WriteString(_Name); // ? '
        writer.WriteString(_Age); // ? '
        // foreach MyLinkedList 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

忘年祭陌 2024-08-28 15:46:55

如果不实现 XmlSerializer ,这是不可能的a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializes.aspx" rel="nofollow noreferrer">IXmlSerialized 或创建公共副本 LinkedList 的 >ListDataContractSerializer 能够执行此操作,但不幸的是它在 .NET 2.0 中不可用。


更新:

这是实现 IXmlSerialized 的示例序列化 LinkedList

public class Foo : IXmlSerializable
{
    public LinkedList<int> List { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        throw new System.NotImplementedException();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("List");
        foreach (var item in List)
        {
            writer.WriteElementString("Item", item.ToString());    
        }
        writer.WriteEndElement();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo
        {
            List = new LinkedList<int>(new[] { 1, 2, 3 })
        };
        var serializer = new XmlSerializer(foo.GetType());
        serializer.Serialize(Console.Out, foo);
    }
}

如您所见,它基本上是手动进行序列化,因此这里的 XmlSerializer 没有太多好处。

It is impossible with XmlSerializer without implementing IXmlSerializable or creating a public duplicate as List<T> from LinkedList<T>. DataContractSerializer is capable of doing this but unfortunately it is not available in .NET 2.0.


UPDATE:

Here's an example of implementing IXmlSerializable to serialize a LinkedList<T>:

public class Foo : IXmlSerializable
{
    public LinkedList<int> List { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        throw new System.NotImplementedException();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("List");
        foreach (var item in List)
        {
            writer.WriteElementString("Item", item.ToString());    
        }
        writer.WriteEndElement();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo
        {
            List = new LinkedList<int>(new[] { 1, 2, 3 })
        };
        var serializer = new XmlSerializer(foo.GetType());
        serializer.Serialize(Console.Out, foo);
    }
}

As you can see it's basically doing the serialization by hand so there's no much benefit of the XmlSerializer here.

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