How to read a XML file and write into List<>?

后端 未结 5 1464
自闭症患者
自闭症患者 2020-12-09 05:09

I have a List<> which I have managed to write into file. Now I am trying to read the same file and write it back to List<>. Is there a

5条回答
  •  余生分开走
    2020-12-09 05:33

    I think the easiest way is to use the XmlSerializer:

    XmlSerializer serializer = new XmlSerializer(typeof(List));
    
    using(FileStream stream = File.OpenWrite("filename"))
    {
        List list = new List();
        serializer.Serialize(stream, list);
    }
    
    using(FileStream stream = File.OpenRead("filename"))
    {
        List dezerializedList = (List)serializer.Deserialize(stream);
    }
    

提交回复
热议问题