I have a List<>
which I wrote into an XML file.
Now I am trying to read the same file and write it back to List<>
.
Is there a method to do that?
I have a List<>
which I wrote into an XML file.
Now I am trying to read the same file and write it back to List<>
.
Is there a method to do that?
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);
}