Пример #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 (xmlDoc.DocumentElement.SelectSingleNode("Connections") is XmlNode connectionsNode)
                {
                    foreach (XmlNode connectionNode in connectionsNode.SelectNodes("Connection"))
                    {
                        ConnectionOptions connectionOptions = new ConnectionOptions();
                        connectionOptions.LoadFromXml(connectionNode);
                        Connections[connectionOptions.Name] = connectionOptions;
                    }
                }

                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();
                        lineConfig.LoadFromXml(lineElem);
                        Lines.Add(lineConfig);
                    }
                }

                FillDriverCodes();
                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = CommonPhrases.LoadAppConfigError + ": " + ex.Message;
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Loads the communication line configuration from the specified file.
        /// </summary>
        public static bool LoadLineConfig(string fileName, int commLineNum, out LineConfig lineConfig, out string errMsg)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(string.Format(CommonPhrases.NamedFileNotFound, fileName));
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                lineConfig = null;

                if (xmlDoc.DocumentElement.SelectSingleNode("Lines") is XmlNode linesNode)
                {
                    foreach (XmlElement lineElem in linesNode.SelectNodes($"Line[@number={commLineNum}]"))
                    {
                        lineConfig = new LineConfig();
                        lineConfig.LoadFromXml(lineElem);
                        break;
                    }
                }

                if (lineConfig == null)
                {
                    errMsg = string.Format(Locale.IsRussian ?
                                           "Конфигурация линии связи {0} не найдена" :
                                           "Communication line {0} configuration not found", commLineNum);
                    return(false);
                }
                else
                {
                    errMsg = "";
                    return(true);
                }
            }
            catch (Exception ex)
            {
                lineConfig = null;
                errMsg     = CommonPhrases.LoadAppConfigError + ": " + ex.Message;
                return(false);
            }
        }
Пример #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();
        }
Пример #4
0
        /// <summary>
        /// Loads the communication line configuration from the specified storage.
        /// </summary>
        public static bool LoadLineConfig(IStorage storage, string fileName, int commLineNum,
                                          out LineConfig lineConfig, out string errMsg)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(new StringReader(storage.ReadText(DataCategory.Config, fileName)));
                lineConfig = null;

                if (xmlDoc.DocumentElement.SelectSingleNode("Lines") is XmlNode linesNode)
                {
                    foreach (XmlElement lineElem in linesNode.SelectNodes($"Line[@number={commLineNum}]"))
                    {
                        lineConfig = new LineConfig();
                        lineConfig.LoadFromXml(lineElem);
                        break;
                    }
                }

                if (lineConfig == null)
                {
                    errMsg = string.Format(Locale.IsRussian ?
                                           "Конфигурация линии связи {0} не найдена" :
                                           "Communication line {0} configuration not found", commLineNum);
                    return(false);
                }
                else
                {
                    errMsg = "";
                    return(true);
                }
            }
            catch (Exception ex)
            {
                lineConfig = null;
                errMsg     = ex.BuildErrorMessage(CommonPhrases.LoadConfigError);
                return(false);
            }
        }