C Write Object or Collection Type Data to xml File

  • 2021-07-10 20:37:01
  • OfStack

This article illustrates how C # writes object or collection type data to an xml file. Share it for your reference. The specific implementation method is as follows:

public static string SerializeToXmlString(object objectToSerialize) {
  MemoryStream memoryStream = new MemoryStream();
  System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
  xmlSerializer.Serialize(memoryStream, objectToSerialize);
  ASCIIEncoding ascii = new ASCIIEncoding();
  return ascii.GetString(memoryStream.ToArray());

I hope this article is helpful to everyone's C # programming.

Related articles: