Esempio n. 1
0
        /// <summary>
        /// Saves the configuration to the specified file.
        /// </summary>
        public bool Save(string fileName, out string errMsg)
        {
            try
            {
                XmlDocument    xmlDoc  = new XmlDocument();
                XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmlDecl);

                XmlElement rootElem = xmlDoc.CreateElement("ScadaServerConfig");
                xmlDoc.AppendChild(rootElem);

                GeneralOptions.SaveToXml(rootElem.AppendElem("GeneralOptions"));
                ListenerOptions.SaveToXml(rootElem.AppendElem("ListenerOptions"));
                PathOptions.SaveToXml(rootElem.AppendElem("PathOptions"));

                XmlElement modulesElem = rootElem.AppendElem("Modules");
                foreach (string moduleCode in ModuleCodes)
                {
                    modulesElem.AppendElem("Module").SetAttribute("code", moduleCode);
                }

                XmlElement archivesElem = rootElem.AppendElem("Archives");
                foreach (ArchiveConfig archiveConfig in Archives)
                {
                    archiveConfig.SaveToXml(archivesElem.AppendElem("Archive"));
                }

                xmlDoc.Save(fileName);
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = CommonPhrases.SaveAppConfigError + ": " + ex.Message;
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the configuration from the specified file.
        /// </summary>
        public bool Load(string fileName, out string errMsg)
        {
            try
            {
                SetToDefault();

                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(string.Format(CommonPhrases.NamedFileNotFound, fileName));
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                XmlElement rootElem = xmlDoc.DocumentElement;

                if (rootElem.SelectSingleNode("GeneralOptions") is XmlNode generalOptionsNode)
                {
                    GeneralOptions.LoadFromXml(generalOptionsNode);
                }

                if (rootElem.SelectSingleNode("ListenerOptions") is XmlNode listenerOptionsNode)
                {
                    ListenerOptions.LoadFromXml(listenerOptionsNode);
                }

                if (rootElem.SelectSingleNode("PathOptions") is XmlNode pathOptionsNode)
                {
                    PathOptions.LoadFromXml(pathOptionsNode);
                }

                HashSet <string> moduleCodes = new HashSet <string>();

                if (rootElem.SelectSingleNode("Modules") is XmlNode modulesNode)
                {
                    foreach (XmlElement moduleElem in modulesNode.SelectNodes("Module"))
                    {
                        string moduleCode = ScadaUtils.RemoveFileNameSuffixes(moduleElem.GetAttribute("code"));

                        if (moduleCodes.Add(moduleCode.ToLowerInvariant())) // check uniqueness
                        {
                            ModuleCodes.Add(moduleCode);
                        }
                    }
                }

                if (rootElem.SelectSingleNode("Archives") is XmlNode archivesNode)
                {
                    foreach (XmlElement archiveElem in archivesNode.SelectNodes("Archive"))
                    {
                        ArchiveConfig archiveConfig = new ArchiveConfig();
                        archiveConfig.LoadFromXml(archiveElem);
                        Archives.Add(archiveConfig);

                        if (archiveConfig.Active && moduleCodes.Add(archiveConfig.Module.ToLowerInvariant()))
                        {
                            ModuleCodes.Add(archiveConfig.Module);
                        }
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = CommonPhrases.LoadAppConfigError + ": " + ex.Message;
                return(false);
            }
        }