0

I got a function that saves the data from a List to a XML file. My question is, how can I actually load the XML file into a List?

My XML file:

<Customers>
<customer ID="0" firstname="xx" lastname="xx" />
</Customers>
L.B
  • 114,136
  • 19
  • 178
  • 224
Jordy144
  • 91
  • 1
  • 1
  • 7
  • I recommend [XmlSerializer](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx) (as I like writing strong types and it supports bi-directional serialization), although [LINQ to XML](http://msdn.microsoft.com/en-us/library/bb387061.aspx) will work well here and doesn't require extra types. – user2864740 Feb 20 '14 at 18:32

1 Answers1

1
using System;
using System.Linq;
using System.Xml.Linq;

public class XmlToList
{
    static void Main()
    {
        string xml = "<Customers><customer ID=\"0\" firstname=\"xx\" lastname=\"xx\" />/Customers>";

        XDocument doc = XDocument.Parse(xml);

        var list = doc.Root.Elements("Customers")
                       .Select(node => node.Value)
                       .ToList();

        foreach (string item in list)
        {
            Console.WriteLine(item);
        }
    }
}

You may refer to this post: https://stackoverflow.com/a/956777/3232673

Community
  • 1
  • 1
Pierre
  • 417
  • 3
  • 9