de \ serialize xml包含对象列表到对象列表

时间:2015-01-12 08:56:41

标签: c# xml serialization deserialization

我试图将对象列表保存到xml文件中。但是当我在保存之前删除之前我会保存。我的意思是如果我保存在"雷达"的xml文件1列表中并保存在" Observer"的保存xml文件列表中。它只保存我保存的最后一个列表。有什么方法可以拯救他们俩? 我要保存的代码:

XmlSerializer serializer = new XmlSerializer(typeof(List<Observer>));

                using (TextWriter writer = new StreamWriter(@"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
                {
                    serializer.Serialize(writer, list);
                }

希望你理解。谢谢你的建议。

编辑: 我创建了包含列表作为属性的新对象。现在所有列表都在一个xml文件中。 对象:

 [Serializable()]
    public class SaveObject
    {
        public List<Radar> listRadars = new List<Radar>();
        public List<Ikun> listIkuns = new List<Ikun>();
        public List<Observer> listObservers = new List<Observer>();
        public XmlSerializer ser;

        public SaveObject(List<Observer> listObservers, List<Ikun> listIkuns,List<Radar> listRadars)
        {
            this.listIkuns = listIkuns;
            this.listRadars = listRadars;
            this.listObservers = listObservers;
        }

        public SaveObject()
        {
            ser = new XmlSerializer(this.GetType());
        }
    }

序列化功能:

public void Serialize(SaveObject SO)
        {
            if (IfPathToSave) // Select save - not auto ! 
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SaveObject));

                using (TextWriter writer = new StreamWriter(FilePath))
                {
                    serializer.Serialize(writer, SO);
                }
            }
            else // auto save ! 
            {

                XmlSerializer serializer = new XmlSerializer(typeof(SaveObject));

                using (TextWriter writer = new StreamWriter(@"C:\Users\user\Desktop\MapSample\bin\Debug\AutoSave.xml"))
                {
                    serializer.Serialize(writer, SO);
                }
            }
        }

希望它能帮助任何人。请享用。 :)

1 个答案:

答案 0 :(得分:0)

如果您有2个列表并且这样做(错误)

var serializer = new XmlSerializer(typeof(List<Observer>));
using (var writer = new StreamWriter(@"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
    serializer.Serialize(writer, listA);
using (var writer = new StreamWriter(@"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
    serializer.Serialize(writer, listB);

然后listB将覆盖ListObserver.xml个内容。

你可以试试这个

var serializer = new XmlSerializer(typeof(List<List<Observer>>));
using (var writer = new StreamWriter(@"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
    serializer.Serialize(writer, new List<List<Observer>>(new[] {listA, listB}));

如果它不起作用(无论出于何种原因),那么只需创建序列化对象

public class SerializationObject
{
    public List<Observer> ListA {get; set;}
    public List<Observer> ListB {get; set;}
}

并使用它

var serializer = new XmlSerializer(typeof(SerializationObject));
using (var writer = new StreamWriter(@"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
    serializer.Serialize(writer, new SerializationObject() { ListA = listA, ListB = listB });