How to read XML file into List<>?

you can use LINQ to XML to read your XML file and bind it to your List.

http://www.mssqltips.com/sqlservertip/1524/reading-xml-documents-using-linq-to-xml/ this link has enough info about it.

this is something I did in past; I hope it helps. I think you want to exactly same thing

    public static List<ProjectMap> MapInfo()
     {

         var maps = from c in XElement.Load(System.Web.Hosting.HostingEnvironment.MapPath("/ProjectMap.xml")).Elements("ProjectMap")
                     select c;
         List<ProjectMap> mapList = new List<ProjectMap>();

         foreach (var item in maps)
         {
             mapList.Add(new ProjectMap() { Project = item.Element("Project").Value, SubProject = item.Element("SubProject").Value, Prefix = item.Element("Prefix").Value, TableID = item.Element("TableID").Value  });

         }
         return mapList;
    }

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

An easy way is

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

public class Test
{
    static void Main()
    {
        string xml = "<Ids><id>1</id><id>2</id></Ids>";

        XDocument doc = XDocument.Parse(xml);

        List<string> list = doc.Root.Elements("id")
                           .Select(element => element.Value)
                           .ToList();


    }
}

You can try this (using System.Xml.Linq)

XDocument xmlDoc = XDocument.Load("yourXMLFile.xml");
var list = xmlDoc.Root.Elements("id")
                           .Select(element => element.Value)
                           .ToList();

Tags:

C#

Xml