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

后端 未结 5 1458
自闭症患者
自闭症患者 2020-12-09 05:09

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

5条回答
  •  感动是毒
    2020-12-09 05:51

    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 MapInfo()
         {
    
             var maps = from c in XElement.Load(System.Web.Hosting.HostingEnvironment.MapPath("/ProjectMap.xml")).Elements("ProjectMap")
                         select c;
             List mapList = new List();
    
             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;
        }
    

提交回复
热议问题