Save List to XML file

后端 未结 6 1942
遥遥无期
遥遥无期 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

    Here are two methods that we use to accomplish this using the XMLSerializer:

     public static T FromXML(string xml)
     {
         using (StringReader stringReader = new StringReader(xml))
         {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
         }
     }
    
     public string ToXML(T obj)
     {
        using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            xmlSerializer.Serialize(stringWriter, obj);
            return stringWriter.ToString();
        }
     }
    

提交回复
热议问题