-3

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user id="12">
    <Nome>pedro</Nome>
    <Email>pedro.roldan@gmail.com</Email>
    <DataNascimento>1976-01-01</DataNascimento>
    <Foto>E:\Trabalho\web\12_7a639ffac9146d346372eb7598e1642f.jpg</Foto>
  </user>
  <user id="13">
    <Nome>DORA</Nome>
    <Email>dora.bilro@gmail.com</Email>
    <DataNascimento>2003-02-12</DataNascimento>
    <Foto>E:\Trabalho\web\13_135bb70e258379ee643d575bd688a692.jpg</Foto>
  </user>
  <user id="14">
    <Nome>dcdc</Nome>
    <Email>rui.moura@ff.pt</Email>
    <DataNascimento>1998-03-25</DataNascimento>
    <Foto>E:\Trabalho\web\14_7a639ffac9146d346372eb7598e1642f.jpg</Foto>
  </user>
</users>

Im trying to import this in asp.net mvc.

What would be the best way to read this into a c# list? Thanks in advance!

1 Answers1

1
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);
}

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

Nick McGinnis
  • 143
  • 13
  • Please do not copy paste an answer to another question. It is exactly the same as the answer of [Enyra](https://stackoverflow.com/questions/9619324/how-to-read-a-xml-file-and-write-into-list#9619418). Instead mark the question as a duplicate. – VDWWD Aug 09 '18 at 14:40