XmlSerializer未将XML反序列化为IEnumerable

问题描述 投票:0回答:1

[虽然引用了Stack Overflow的一篇文章,XmlSerializer won't serialize IEnumerable,但我有以下代码可以正确地序列化为XML,但是反序列化后,IEnumerable会变空。

using System.Xml.Linq;
using System.Xml.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;

public class Hello{
    public static void Main(){

        // <!--- Serializing part -->
        Glossary glossary1 = new Glossary();
        glossary1.Term = "Test1";
        glossary1.Definition = "Test1";
        Glossary glossary2 = new Glossary();
        glossary2.Term = "Test2";
        glossary2.Definition = "Test2";
        List<Glossary> glossaryList = new List<Glossary>();
        glossaryList.Add(glossary1);
        glossaryList.Add(glossary2);
        GlossaryCollection glossary = new GlossaryCollection(glossaryList);

        XDocument doc = new XDocument();
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(GlossaryCollection));
        using (var writer = doc.CreateWriter()) {
            xmlSerializer.Serialize(writer, glossary);
        }

        doc.Save("test.xml");
        XDocument doc2 = XDocument.Load("test.xml");
        Console.WriteLine(glossary.glossaryList.Count());
        Console.WriteLine(doc2.ToString());
        //So far looks good - serializing works fine!
        // </!--- Serializing part -->      
        // <!--- De-Serializing part -->                

        GlossaryCollection temp2;
        xmlSerializer = new XmlSerializer(typeof(GlossaryCollection));
        using (var reader = doc2.Root.CreateReader()) {
            var temp = xmlSerializer.Deserialize(reader);
            temp2 = (GlossaryCollection) temp;
        }

        Console.WriteLine(temp2.glossaryList.Count()); // Results in 0, should be 2... :(
        // </!--- De-Serializing part -->               
    }
    [XmlRoot]
            public class GlossaryCollection {
                [XmlIgnore]
                    public IEnumerable<Glossary> glossaryList;
                [XmlElement]    
                    public List<Glossary> GlossarySurrogate { get { return glossaryList.ToList(); } set { glossaryList = value; } }  // https://stackoverflow.com/questions/9102234/xmlserializer-wont-serialize-ienumerable   
                public GlossaryCollection() {
                    glossaryList = new List<Glossary>();
                }
                public GlossaryCollection(IEnumerable<Glossary> glossaryList) {
                    this.glossaryList = glossaryList;
                }
            }
        [XmlType("Glossary")]
                public class Glossary
                {
                        public string Id { get; set; }
                        public string Term { get; set; }
                        public string Definition { get; set; }
                }   
}

更新:基于Iridium的回答,似乎无法将IEnumerable与XmlSerializer一起使用,因为它每次都返回一个新列表,因此采用了转换为List的方法。但是,如果有一种解决方案(即使是hacky),也可以将词汇表列表保持为IEnumerable,即使是纯粹出于好奇,也请让我知道。

c# xml-serialization xmlserializer
1个回答
3
投票

[当您的XML序列化类型具有List<T>类型的属性时,XmlSerializerget该属性的值,然后调用.Add(...)以添加反序列化的元素(即它具有not [ C0]属性)。

由于set属性的getter每次您返回GlossarySurrogate时都会返回一个新列表,因此XML序列化程序对结果所做的更改将丢失。

如果要使其正常工作,则需要将get字段转换为glossaryList,并直接从List<Glossary>吸气剂返回它,而无需对其调用GlossarySurrogate

© www.soinside.com 2019 - 2024. All rights reserved.