本文介绍了如何将XML序列化的LinkedList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 2

其实有方法,以XML序列化名单,其中,T> 。如果我有一个对象,有一个公共的LinkedList< T> 成员

而无需创建一个公共副本为名单,其中,T> 的LinkedList< T> 。也许一种方式来控制XML序列化类似二进制(OnSerializing,OnDeserializing)。

将不可能XML与公共序列化对象的LinkedList< T> 成员

编辑:

这是测试的例子与IXmlSerializable的解决

 使用的System.Xml.Serialization;

公共类Foo:IXmlSerializable的{
私人诠释_id;
私人字符串_Name;

公共字符串名称{
{返回_Name; }
集合{_Name =价值; }
}

私人诠释_Age;
公共字符串年龄{
{返回_Age; }
集合{_Age =价值; }
}

私人链表<酒吧GT; _linkedList =新的LinkedList<酒吧>();
[XmlArray()]
公开名单<酒吧GT; MyLinkedList {
{返回_linkedList; }
集合{_linkedList =价值; }
}

公共System.Xml.Schema.XmlSchema的getSchema(){
返回null;
}

公共无效的ReadXml(System.Xml.XmlReader阅读器){
_name = reader.ReadString(); //? 
_Age = reader.ReadString(); //? 
        //的foreach MyLinkedList
}

公共无效中WriteXML(System.Xml.XmlWriter作家){
writer.WriteString(_Name); //? 
writer.WriteString(_Age); //? 
        //的foreach MyLinkedList
}
}
 

解决方案

这是的没有实施的或创建一个公共副本为名单,其中,T> 的LinkedList< T> 。 DataContractSerializer的是可以这样做的,但遗憾的是它不提供.NET 2.0。


更新:

下面是实施的IXmlSerializable 的例子序列化的LinkedList< T>

 公共类Foo:的IXmlSerializable
{
    公共链表< INT>清单{获得;组; }

    公众的XmlSchema的getSchema()
    {
        返回null;
    }

    公共无效的ReadXml(XmlReader的读者)
    {
        抛出新System.NotImplementedException();
    }

    公共无效中WriteXML(XmlWriter的作家)
    {
        writer.WriteStartElement(目录);
        的foreach(在列表VAR项)
        {
            writer.WriteElementString(项目,item.ToString());
        }
        writer.WriteEndElement();
    }
}

类节目
{
    静态无效的主要(字串[] args)
    {
        无功富=新的Foo
        {
            列表=新链表&其中; INT>(新[] {1,2,3})
        };
        无功序列化=新的XmlSerializer(foo.GetType());
        serializer.Serialize(Console.Out,FOO);
    }
}
 

正如你可以看到它基本上做手工序列化所以有没有多少好处的的XmlSerializer 在这里。

.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 
    }
}
解决方案

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.

这篇关于如何将XML序列化的LinkedList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 03:45