Save List to XML file

后端 未结 6 1924
遥遥无期
遥遥无期 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 13:46

    Using the code below (Class T Taken from your code snippet) you will be able to serialize into an XML file with ease, and without the hassle of implementing ISerializable

    [Serializable()]
    public class T
    {
        public int Id {get; set;}
        public string property1 {get; set;}
        public string property2 {get; set;}
    }
    
    ...
    
    List data = new List()
    
    ... // populate the list
    
    //create the serialiser to create the xml
    XmlSerializer serialiser = new XmlSerializer(typeof(List));
    
    // Create the TextWriter for the serialiser to use
    TextWriter filestream = new StreamWriter(@"C:\output.xml");
    
    //write to the file
    serialiser.Serialize(filestream , data);
    
    // Close the file
    filestream.Close();
    

提交回复
热议问题