// Copyright (C) Stichting Deltares 2018. 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.IO; using System.Xml.Serialization; using Deltares.DamEngine.Io.XmlOutput; using Input = Deltares.DamEngine.Io.XmlInput.Input; namespace Deltares.DamEngine.Io { public static class DamXmlSerialization { /// /// Saves the input as XML. /// /// The filename. /// The input. public static void SaveInputAsXmlFile(string filename, Input input) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Input)); TextWriter writer = new StreamWriter(filename); xmlSerializer.Serialize(writer, input); writer.Close(); } /// /// Loads the input from XML. /// /// The filename. /// Input object public static Input LoadInputFromXmlFile(string filename) { Input input; XmlSerializer xmlSerializer = new XmlSerializer(typeof(Input)); FileStream fs = new FileStream(filename, FileMode.Open); input = (Input)xmlSerializer.Deserialize(fs); return input; } /// /// Saves the input as XML string. /// /// The input. /// xm string public static string SaveInputAsXmlString(Input input) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Input)); TextWriter writer = new StringWriter(); xmlSerializer.Serialize(writer, input); return writer.ToString(); } /// /// Loads the input from XML string. /// /// The XML string. /// Input object public static Input LoadInputFromXmlString(string xmlString) { Input input; XmlSerializer xmlSerializer = new XmlSerializer(typeof(Input)); StringReader reader = new StringReader(xmlString); input = (Input)xmlSerializer.Deserialize(reader); return input; } /// /// Saves the output as XML. /// /// The filename. /// The output. public static void SaveOutputAsXmlFile(string filename, Output output) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Output)); TextWriter writer = new StreamWriter(filename); xmlSerializer.Serialize(writer, output); writer.Close(); } /// /// Loads the output from XML. /// /// The filename. /// Output object public static Output LoadOutputFromXmlFile(string filename) { Output output; XmlSerializer xmlSerializer = new XmlSerializer(typeof(Output)); FileStream fs = new FileStream(filename, FileMode.Open); output = (Output)xmlSerializer.Deserialize(fs); return output; } /// /// Saves the output as XML string. /// /// The output. /// public static string SaveOutputAsXmlString(Output output) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Output)); TextWriter writer = new StringWriter(); xmlSerializer.Serialize(writer, output); return writer.ToString(); } /// /// Loads the output from XML string. /// /// The output XML. /// public static Output LoadOutputFromXmlString(string outputXml) { Output output; XmlSerializer xmlSerializer = new XmlSerializer(typeof(Output)); StringReader reader = new StringReader(outputXml); output = (Output)xmlSerializer.Deserialize(reader); return output; } } }