public void SaveCanalConfiguration(Canal canal, string saveLocation)
        {
            if (!Directory.Exists(saveLocation))
            {
                Directory.CreateDirectory(saveLocation);
            }

            string        xml;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(CanalConfiguration));

            CanalConfigurationConverter converter = new CanalConfigurationConverter();

            CanalConfiguration canalConfiguration = converter.ConvertBack(canal);

            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    xmlSerializer.Serialize(writer, canalConfiguration);
                    xml = sww.ToString(); // Your XML
                }

                File.WriteAllText(Path.Combine(saveLocation, "CanalConfiguration.xml"), xml);
            }
        }
        public ICanal LoadCanalConfiguration(string configurationLocation)
        {
            string fileContent;

            if (string.IsNullOrEmpty(configurationLocation))
            {
                throw new ArgumentNullException(nameof(configurationLocation), "No file location was passed");
            }

            using (StreamReader streamReader = File.OpenText(configurationLocation))
            {
                fileContent = streamReader.ReadToEnd();
            }

            if (!string.IsNullOrEmpty(fileContent))
            {
                XmlSerializer      xmlSerializer = new XmlSerializer(typeof(CanalConfiguration));
                CanalConfiguration deserializedConfiguration;

                using (StringReader sr = new StringReader(fileContent))
                {
                    deserializedConfiguration = (CanalConfiguration)xmlSerializer.Deserialize(sr);
                }

                if (deserializedConfiguration != null)
                {
                    CanalConfigurationConverter converter = new CanalConfigurationConverter();
                    Canal canal = converter.Convert(deserializedConfiguration);
                    CanalManager.SetCanal(canal);

                    return(canal);
                }
            }

            throw new ArgumentException($"It was not possible to load canal configuration from <{configurationLocation}>");
        }
        public void TrapezoidSerializingTest()
        {
            //  Arrange
            XmlSerializer      xmlSerializer      = new XmlSerializer(typeof(CanalConfiguration));
            CanalConfiguration canalConfiguration = new CanalConfiguration();

            canalConfiguration.ConfigId = "TestConfig";

            CanalNode initNode = new CanalNode()
            {
                NodeId = "InitNode"
            };
            CanalNode endNode = new CanalNode()
            {
                NodeId = "EndNode"
            };
            CanalArrow arrow = new CanalArrow()
            {
                ArrowId    = "TestArrow",
                Length     = 500.0,
                FromNodeId = initNode.NodeId,
                ToNodeId   = endNode.NodeId
            };

            arrow.CanalSection = new TrapezoidalSectionConfiguration()
            {
                Roughness = 0.028,
                Slope     = 0.001,
                Width     = 5.0,
                Incline   = 0.5
            };
            canalConfiguration.Arrows = new List <CanalArrow>()
            {
                arrow
            };
            canalConfiguration.Nodes = new List <CanalNode>()
            {
                initNode, endNode
            };
            var xml = "";

            //  Act
            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    xmlSerializer.Serialize(writer, canalConfiguration);
                    xml = sww.ToString(); // Your XML
                }
            }

            //  Assert
            Assert.IsTrue(xml != null);
            CanalConfiguration deserializedConfiguration = null;

            using (StringReader sr = new StringReader(xml))
            {
                deserializedConfiguration = (CanalConfiguration)xmlSerializer.Deserialize(sr);
            }

            Assert.IsTrue(deserializedConfiguration != null);
            Assert.AreEqual(2, deserializedConfiguration.Nodes.Count);
            Assert.AreEqual(1, deserializedConfiguration.Arrows.Count);

            CanalConfigurationConverter converter = new CanalConfigurationConverter();

            var data = converter.Convert(deserializedConfiguration);

            Assert.IsTrue(data != null);
            Assert.AreEqual(2, data.CanelEdges.Count);
            Assert.AreEqual(1, data.CanalStretches.Count);

            canalConfiguration = converter.ConvertBack(data);

            Assert.IsTrue(canalConfiguration != null);
            Assert.AreEqual(2, canalConfiguration.Nodes.Count);
            Assert.AreEqual(1, canalConfiguration.Arrows.Count);
        }
        public void RectangularWithSluidgeGateCompositeSerializingTest()
        {
            //  Arrange
            XmlSerializer      xmlSerializer      = new XmlSerializer(typeof(CanalConfiguration));
            CanalConfiguration canalConfiguration = new CanalConfiguration();

            canalConfiguration.ConfigId = "TestConfig";

            SluiceGateNode initNode = new SluiceGateNode()
            {
                NodeId = "InitNode",
                ContractionCoefficient = 0.8,
                GateOpening            = 0.125,
                RetainedWaterLevel     = 2,
                GateWidth = 2
            };

            CanalNode middleNode = new CanalNode()
            {
                NodeId = "MiddleNode"
            };

            CanalNode endNode = new CanalNode()
            {
                NodeId = "EndNode"
            };
            CanalArrow arrow1 = new CanalArrow()
            {
                ArrowId    = "TestArrow1",
                Length     = 600.0,
                FromNodeId = initNode.NodeId,
                ToNodeId   = middleNode.NodeId
            };

            arrow1.CanalSection = new RectangularSectionConfiguration()
            {
                Roughness = 0.014,
                Slope     = 0.0004,
                Width     = 2.0
            };

            CanalArrow arrow2 = new CanalArrow()
            {
                ArrowId    = "TestArrow2",
                Length     = 600.0,
                FromNodeId = middleNode.NodeId,
                ToNodeId   = endNode.NodeId
            };

            arrow2.CanalSection = new RectangularSectionConfiguration()
            {
                Roughness = 0.014,
                Slope     = 0.0045,
                Width     = 2.0
            };

            canalConfiguration.Arrows = new List <CanalArrow>()
            {
                arrow1, arrow2
            };
            canalConfiguration.Nodes = new List <CanalNode>()
            {
                initNode, middleNode, endNode
            };
            var xml = "";

            //  Act
            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    xmlSerializer.Serialize(writer, canalConfiguration);
                    xml = sww.ToString(); // Your XML
                }
            }

            //  Assert
            Assert.IsTrue(xml != null);

            CanalConfiguration deserializedConfiguration = null;

            using (StringReader sr = new StringReader(xml))
            {
                deserializedConfiguration = (CanalConfiguration)xmlSerializer.Deserialize(sr);
            }

            Assert.IsTrue(deserializedConfiguration != null);
            Assert.AreEqual(3, deserializedConfiguration.Nodes.Count);
            Assert.AreEqual(2, deserializedConfiguration.Arrows.Count);

            CanalConfigurationConverter converter = new CanalConfigurationConverter();

            var data = converter.Convert(deserializedConfiguration);

            Assert.IsTrue(data != null);
            Assert.AreEqual(3, data.CanelEdges.Count);
            Assert.AreEqual(2, data.CanalStretches.Count);

            canalConfiguration = converter.ConvertBack(data);

            Assert.IsTrue(canalConfiguration != null);
            Assert.AreEqual(3, canalConfiguration.Nodes.Count);
            Assert.AreEqual(2, canalConfiguration.Arrows.Count);
        }