XML Serialization Helpers

public static class Serialization { private static readonly Encoding _defaultEncoding = Encoding.UTF8; public static T DeserializeXmlFile<T>(string fileName) { if (!File.Exists(fileName)) throw new FileNotFoundException(); var serializer = new XmlSerializer(typeof(T)); using (var xmlReader = new XmlTextReader(fileName)) { return (T)serializer.Deserialize(xmlReader); } } public static T Deserialize<T>(TextReader textReader) { if (textReader == null) throw new ArgumentNullException(“textReader”); var serializer = […]

C# Record Serialization

With .NET 5, you get a new concept called record. Lots of cool things, one of which is that you can serialize and deserialize with JSON just like a class, except you don’t need to define properties one at a time, or even the body of the type. Apparently XML serialization doesn’t quite work, so […]