Save List to XML file

后端 未结 6 1929
遥遥无期
遥遥无期 2021-02-05 13:15

I want to save records fetched from database in an XML file,
take x number of records from XML file into a custom collection List
process them and

6条回答
  •  感情败类
    2021-02-05 14:09

    While you could use a serializer - and many times this is the right answer - I personally would use Linq to XML which would allow you to be more flexible on how your XML should look like, i.e. to create the following XML from a collection foos based on your class:

    
      
      
    
    

    You could use:

    var xml = new XElement("Foos", foos.Select( x=> new XElement("foo", 
                                                    new XAttribute("Id", x.Id), 
                                                    new XAttribute("property1", x.property1), 
                                                    new XAttribute("property2", x.property2))));
    

提交回复
热议问题