如何读取XML文件并将其写入List<&gt ;? [英] How to read a XML file and write into List<>?

查看:133
本文介绍了如何读取XML文件并将其写入List<&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List<> ,我已设法将其写入文件。
现在,我试图读取相同的文件,并将其写回到 List<>
有这样做的方法吗?
有人可以帮助我吗?
有用吗?

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 method to do that? Can anyone help me with the start? Will be useful?

推荐答案

我认为最简单的方法是使用 XmlSerializer

I think the easiest way is to use the XmlSerializer:

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

using(FileStream stream = File.OpenWrite("filename"))
{
    List<MyClass> list = new List<MyClass>();
    serializer.Serialize(stream, list);
}

using(FileStream stream = File.OpenRead("filename"))
{
    List<MyClass> dezerializedList = (List<MyClass>)serializer.Deserialize(stream);
}

这篇关于如何读取XML文件并将其写入List&lt;&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆