Skip to content

Instantly share code, notes, and snippets.

@mirmostafa
Created March 3, 2022 08:34
Show Gist options
  • Select an option

  • Save mirmostafa/0a67037b7ef195590319d8180c1040ed to your computer and use it in GitHub Desktop.

Select an option

Save mirmostafa/0a67037b7ef195590319d8180c1040ed to your computer and use it in GitHub Desktop.
XML Serializer
public static class XmlSerializer
{
public static void Serialize<T>(T? o, string path!!, bool indent = true)
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using var writer = new StreamWriter(path);
using var xmlWriter = System.Xml.XmlWriter.Create(writer, new System.Xml.XmlWriterSettings { Indent = indent });
serializer.Serialize(xmlWriter, o);
}
public static T? Desrialize<T>(string path!!)
{
using var file = new FileStream(path, FileMode.Open);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
var result = serializer.Deserialize(file);
return result is null ? default : (T)result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment