public override byte[] Save(Project project) { // Wrap the project in a top-level root element with some info about the XML file // format version. Note that each serializer has its own implementation of storing this metadata var rootElement = new Root() { SaveVersion = CurrentSaveFormatVersion, Watermark = ProjectSerializer.Watermark, Project = project, }; var xmlStr = XmlSerializerSupport.GetSerializer().Create().Serialize( new XmlWriterSettings { Indent = true }, rootElement); var finalBytes = Encoding.UTF8.GetBytes(xmlStr); // if you want some sanity checking, run this to verify everything saved correctly // DebugVerifyProjectEquality(project, finalBytes); // end debug return(finalBytes); }
public override (Project project, string warning) Load(byte[] data) { // TODO: it would be much more user-friendly/reliable if we could deserialize the // Root element ALONE first, check for valid version/watermark, and only then try // to deserialize the rest of the doc. // // Also, we can do data migrations based on versioning, and ExtendedXmlSerializer var text = Encoding.UTF8.GetString(data); var root = XmlSerializerSupport.GetSerializer().Create().Deserialize <Root>(text); if (root.Watermark != Watermark) { throw new InvalidDataException( "This file doesn't appear to be a valid DiztinGUIsh XML file (missing/invalid watermark element in XML)"); } if (root.SaveVersion > CurrentSaveFormatVersion) { throw new InvalidDataException( $"Save file version is newer than this version of DiztinGUIsh, likely can't be opened safely. This save file version = '{root.SaveVersion}', our highest supported version is {CurrentSaveFormatVersion}"); } // Apply any migrations here for older save file formats. Right now, // there aren't any because we're on the first revision. // The XML serialization might be fairly forgiving of most kinds of changes, // so you may not have to write migrations unless properties are renamed or deleted. if (root.SaveVersion < CurrentSaveFormatVersion) { throw new InvalidDataException( $"Save file version is newer than this version of DiztinGUIsh, likely can't be opened safely. This save file version = '{root.SaveVersion}', our highest supported version is {CurrentSaveFormatVersion}"); } var project = root.Project; var warning = ""; return(project, warning); }