// Copyright (C) Stichting Deltares 2020. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.IO; using System.Reflection; using System.Xml.Serialization; namespace Deltares.LayerOnSlopeTool.Io { public class MStabDamXmlSerializer { /// /// Saves the DamMStabDoc as XML. /// /// The filename. /// The DamMStabDoc. public static void SaveDamMStabDocAsXmlFile(string filename, DamMStabDoc DamMStabDoc) { DamMStabDoc.FileIdentification = CreateFileIdentification(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(DamMStabDoc)); TextWriter writer = new StreamWriter(filename); xmlSerializer.Serialize(writer, DamMStabDoc); writer.Close(); } /// /// Loads the DamMStabDoc from XML. /// /// The filename. /// DamMStabDoc object public static DamMStabDoc LoadDamMStabDocFromXmlFile(string filename) { DamMStabDoc DamMStabDoc; XmlSerializer xmlSerializer = new XmlSerializer(typeof(DamMStabDoc)); FileStream fs = new FileStream(filename, FileMode.Open); DamMStabDoc = (DamMStabDoc)xmlSerializer.Deserialize(fs); return DamMStabDoc; } /// /// Saves the DamMStabDoc as XML string. /// /// The DamMStabDoc. /// xm string public static string SaveDamMStabDocAsXmlString(DamMStabDoc DamMStabDoc) { DamMStabDoc.FileIdentification = CreateFileIdentification(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(DamMStabDoc)); TextWriter writer = new StringWriter(); xmlSerializer.Serialize(writer, DamMStabDoc); return writer.ToString(); } /// /// Loads the DamMStabDoc from XML string. /// /// The XML string. /// DamMStabDoc object public static DamMStabDoc LoadDamMStabDocFromXmlString(string xmlString) { DamMStabDoc DamMStabDoc; XmlSerializer xmlSerializer = new XmlSerializer(typeof(DamMStabDoc)); StringReader reader = new StringReader(xmlString); DamMStabDoc = (DamMStabDoc)xmlSerializer.Deserialize(reader); return DamMStabDoc; } private static DamMStabDocFileIdentification CreateFileIdentification() { DamMStabDocFileIdentification damMStabDocFileIdentification = new DamMStabDocFileIdentification(); var hostAssembly = Assembly.GetEntryAssembly(); if (hostAssembly == null) { hostAssembly = Assembly.GetExecutingAssembly(); } damMStabDocFileIdentification.Application = hostAssembly.GetName().Name; damMStabDocFileIdentification.Version = hostAssembly.GetName().Version.ToString(); damMStabDocFileIdentification.Created = DateTime.Now.ToShortDateString() + " "+ DateTime.Now.ToShortTimeString(); damMStabDocFileIdentification.Company = "Deltares"; return damMStabDocFileIdentification; } } }