using System.IO; using System.Xml; using System.Xml.Serialization; namespace SabreTools.Serialization { /// /// XML serializer for nullable types /// public abstract class XmlSerializer { #region Deserialization /// /// Deserializes an XML file to the defined type /// /// Path to the file to deserialize /// Deserialized data on success, null on failure public static T? Deserialize(string path) { try { using var stream = PathProcessor.OpenStream(path); return Deserialize(stream); } catch { // TODO: Handle logging the exception return default; } } /// /// Deserializes an XML file in a stream to the defined type /// /// Stream to deserialize /// Deserialized data on success, null on failure public static T? Deserialize(Stream? stream) { try { // If the stream is null if (stream == null) return default; // Setup the serializer and the reader var serializer = new XmlSerializer(typeof(T)); var settings = new XmlReaderSettings { CheckCharacters = false, DtdProcessing = DtdProcessing.Ignore, }; var streamReader = new StreamReader(stream); var xmlReader = XmlReader.Create(streamReader, settings); // Perform the deserialization and return return (T?)serializer.Deserialize(xmlReader); } catch { // TODO: Handle logging the exception return default; } } #endregion #region Serialization /// /// Serializes the defined type to an XML file /// /// Data to serialize /// Path to the file to serialize to /// True on successful serialization, false otherwise public static bool SerializeToFile(T? obj, string path) { try { using var stream = SerializeToStream(obj); if (stream == null) return false; using var fs = File.OpenWrite(path); stream.CopyTo(fs); return true; } catch { // TODO: Handle logging the exception return false; } } /// /// Serializes the defined type to a stream /// /// Data to serialize /// Stream containing serialized data on success, null otherwise public static Stream? SerializeToStream(T? obj) { try { // If the object is null if (obj == null) return null; // Setup the serializer and the reader var serializer = new XmlSerializer(typeof(T)); var settings = new XmlWriterSettings { CheckCharacters = false, }; var stream = new MemoryStream(); var streamWriter = new StreamWriter(stream); var xmlWriter = XmlWriter.Create(streamWriter, settings); // Perform the deserialization and return serializer.Serialize(xmlWriter, obj); return stream; } catch { // TODO: Handle logging the exception return null; } } #endregion } }