コード例 #1
0
ファイル: GCDProject.cs プロジェクト: tarinishukla/gcd
        public void Save()
        {
            XmlDocument xmlDoc     = new XmlDocument();
            XmlNode     nodProject = xmlDoc.CreateElement("Project");

            xmlDoc.AppendChild(nodProject);

            // Create an XML declaration.
            XmlDeclaration xmldecl;

            xmldecl = xmlDoc.CreateXmlDeclaration("1.0", null, null);
            xmlDoc.InsertBefore(xmldecl, xmlDoc.DocumentElement);

            nodProject.AppendChild(xmlDoc.CreateElement("Name")).InnerText            = Name;
            nodProject.AppendChild(xmlDoc.CreateElement("DateTimeCreated")).InnerText = DateTimeCreated.ToString("o");
            nodProject.AppendChild(xmlDoc.CreateElement("GCDVersion")).InnerText      = GCDVersion;

            if (OnlineParams.Count > 0)
            {
                XmlNode nodUpload = nodProject.AppendChild(xmlDoc.CreateElement("Online"));
                foreach (KeyValuePair <string, string> kvp in OnlineParams)
                {
                    XmlNode kvpItem = nodUpload.AppendChild(xmlDoc.CreateElement(kvp.Key));
                    kvpItem.InnerText = kvp.Value;
                }
            }

            XmlNode nodDescription = nodProject.AppendChild(xmlDoc.CreateElement("Description"));

            if (!string.IsNullOrEmpty(Description))
            {
                nodDescription.InnerText = Description;
            }

            XmlNode nodUnits = nodProject.AppendChild(xmlDoc.CreateElement("Units"));

            nodUnits.AppendChild(xmlDoc.CreateElement("Horizontal")).InnerText = Units.HorizUnit.ToString();
            nodUnits.AppendChild(xmlDoc.CreateElement("Vertical")).InnerText   = Units.VertUnit.ToString();
            nodUnits.AppendChild(xmlDoc.CreateElement("Area")).InnerText       = Units.ArUnit.ToString();
            nodUnits.AppendChild(xmlDoc.CreateElement("Volume")).InnerText     = Units.VolUnit.ToString();

            XmlNode nodArea = nodProject.AppendChild(xmlDoc.CreateElement("CellArea"));

            if (CellArea.As(Units.ArUnit) > 0)
            {
                nodArea.InnerText = CellArea.As(Units.ArUnit).ToString("R", CultureInfo.InvariantCulture);
            }

            if (DEMSurveys.Count > 0)
            {
                XmlNode nodDEMs = nodProject.AppendChild(xmlDoc.CreateElement("DEMSurveys"));
                foreach (DEMSurvey dem in DEMSurveys)
                {
                    XmlNode nodItem = nodDEMs.AppendChild(xmlDoc.CreateElement("DEM"));
                    dem.Serialize(nodItem);
                }
            }

            if (ReferenceSurfaces.Count > 0)
            {
                XmlNode nodSurfaces = nodProject.AppendChild(xmlDoc.CreateElement("ReferenceSurfaces"));
                foreach (Surface surf in ReferenceSurfaces)
                {
                    XmlNode nodItem = nodSurfaces.AppendChild(xmlDoc.CreateElement("ReferenceSurface"));
                    surf.Serialize(nodItem);
                }
            }

            if (Masks.Count > 0)
            {
                XmlNode nodMasks = nodProject.AppendChild(xmlDoc.CreateElement("Masks"));
                Masks.ForEach(x => x.Serialize(nodMasks));
            }

            if (ProfileRoutes.Count > 0)
            {
                XmlNode nodRoutes = nodProject.AppendChild(xmlDoc.CreateElement("ProfileRoutes"));
                ProfileRoutes.ForEach(x => x.Serialize(nodRoutes));
            }

            if (DoDs.Count > 0)
            {
                XmlNode nodDoDs = nodProject.AppendChild(xmlDoc.CreateElement("DoDs"));
                foreach (DoDBase dod in DoDs)
                {
                    dod.Serialize(nodDoDs);
                }
            }

            if (InterComparisons.Count > 0)
            {
                XmlNode nodInter = nodProject.AppendChild(xmlDoc.CreateElement("InterComparisons"));
                InterComparisons.ForEach(x => x.Serialize(nodInter));
            }

            if (MetaData.Count > 0)
            {
                XmlNode nodMetaData = nodProject.AppendChild(xmlDoc.CreateElement("MetaData"));
                foreach (KeyValuePair <string, string> item in MetaData)
                {
                    XmlNode nodItem = nodMetaData.AppendChild(xmlDoc.CreateElement("Item"));
                    nodItem.AppendChild(xmlDoc.CreateElement("Key")).InnerText   = item.Key;
                    nodItem.AppendChild(xmlDoc.CreateElement("Value")).InnerText = item.Value;
                }
            }

            xmlDoc.Save(ProjectFile.FullName);

            try
            {
                SaveRiverscapesProject(ProjectFile);
            }
            catch (Exception ex)
            {
                // Fail silently
                Console.WriteLine("Error saving riverscapes project file.");
                Console.WriteLine(ex.Message);
            }
        }