Esempio n. 1
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("ConnectionOptions") is XmlNode connectionOptionsNode)
                {
                    ConnectionOptions.LoadFromXml(connectionOptionsNode);
                }

                if (rootElem.SelectSingleNode("Lines") is XmlNode linesNode)
                {
                    foreach (XmlElement lineElem in linesNode.SelectNodes("Line"))
                    {
                        LineConfig lineConfig = new LineConfig();
                        lineConfig.LoadFromXml(lineElem);
                        Lines.Add(lineConfig);
                    }
                }

                FillDriverCodes();
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = CommonPhrases.LoadAppConfigError + ": " + ex.Message;
                return(false);
            }
        }
Esempio n. 2
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"));

                XmlElement connectionsElem = rootElem.AppendElem("Connections");
                foreach (ConnectionOptions connectionOptions in Connections.Values)
                {
                    connectionOptions.SaveToXml(connectionsElem.AppendElem("Connection"));
                }

                XmlElement dataSourcesElem = rootElem.AppendElem("DataSources");
                foreach (DataSourceConfig dataSourceConfig in DataSources)
                {
                    dataSourceConfig.SaveToXml(dataSourcesElem.AppendElem("DataSource"));
                }

                XmlElement linesElem = rootElem.AppendElem("Lines");
                foreach (LineConfig lineConfig in Lines)
                {
                    lineConfig.SaveToXml(linesElem.AppendElem("Line"));
                }

                xmlDoc.Save(fileName);
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = CommonPhrases.SaveAppConfigError + ": " + ex.Message;
                return(false);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Loads the configuration from the specified reader.
        /// </summary>
        protected override void Load(TextReader reader)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(reader);
            XmlElement rootElem = xmlDoc.DocumentElement;

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

            if (rootElem.SelectSingleNode("ConnectionOptions") is XmlNode connectionOptionsNode)
            {
                ConnectionOptions.LoadFromXml(connectionOptionsNode);
            }

            if (rootElem.SelectSingleNode("DataSources") is XmlNode dataSourcesNode)
            {
                foreach (XmlElement dataSourceElem in dataSourcesNode.SelectNodes("DataSource"))
                {
                    DataSourceConfig dataSourceConfig = new DataSourceConfig();
                    dataSourceConfig.LoadFromXml(dataSourceElem);
                    DataSources.Add(dataSourceConfig);
                }
            }

            if (rootElem.SelectSingleNode("Lines") is XmlNode linesNode)
            {
                foreach (XmlElement lineElem in linesNode.SelectNodes("Line"))
                {
                    LineConfig lineConfig = new LineConfig {
                        Parent = this
                    };
                    lineConfig.LoadFromXml(lineElem);
                    Lines.Add(lineConfig);
                }
            }

            FillDriverCodes();
        }