/// <summary> /// Clears the configuration. /// </summary> private void Clear() { ExtensionCodes = new List <string>(); FileAssociations = new SortedList <string, string>(); ChannelNumberingOptions = new ChannelNumberingOptions(); CustomOptions = new SortedList <string, OptionList>(); }
/// <summary> /// Loads the configuration from the specified file. /// </summary> public bool Load(string fileName, out string errMsg) { try { Clear(); XmlDocument xmlDoc = new(); xmlDoc.Load(fileName); XmlElement rootElem = xmlDoc.DocumentElement; if (rootElem.SelectSingleNode("Extensions") is XmlNode modulesNode) { HashSet <string> extensionCodes = new(); foreach (XmlElement moduleElem in modulesNode.SelectNodes("Extension")) { string moduleCode = ScadaUtils.RemoveFileNameSuffixes(moduleElem.GetAttribute("code")); if (extensionCodes.Add(moduleCode.ToLowerInvariant())) // check uniqueness { ExtensionCodes.Add(moduleCode); } } } if (rootElem.SelectSingleNode("FileAssociations") is XmlNode fileAssociationsNode) { foreach (XmlElement associationElem in fileAssociationsNode.SelectNodes("Association")) { string ext = associationElem.GetAttrAsString("ext").ToLowerInvariant(); string path = associationElem.GetAttrAsString("path"); FileAssociations[ext] = path; } } if (rootElem.SelectSingleNode("ChannelNumberingOptions") is XmlNode channelNumberingOptionsNode) { ChannelNumberingOptions.LoadFromXml(channelNumberingOptionsNode); } if (rootElem.SelectSingleNode("CustomOptions") is XmlNode customOptionsNode) { foreach (XmlElement optionGroupElem in customOptionsNode.SelectNodes("OptionGroup")) { OptionList optionList = new(); optionList.LoadFromXml(optionGroupElem); CustomOptions[optionGroupElem.GetAttrAsString("name")] = optionList; } } errMsg = ""; return(true); } catch (Exception ex) { errMsg = ex.BuildErrorMessage(CommonPhrases.LoadConfigError); return(false); } }
/// <summary> /// Saves the configuration to the specified file. /// </summary> public bool Save(string fileName, out string errMsg) { try { XmlDocument xmlDoc = new(); XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(xmlDecl); XmlElement rootElem = xmlDoc.CreateElement("ScadaAdminConfig"); xmlDoc.AppendChild(rootElem); XmlElement extensionsElem = rootElem.AppendElem("Extensions"); foreach (string extensionCode in ExtensionCodes) { extensionsElem.AppendElem("Extension").SetAttribute("code", extensionCode); } XmlElement fileAssociationsElem = rootElem.AppendElem("FileAssociations"); foreach (KeyValuePair <string, string> pair in FileAssociations) { XmlElement associationElem = fileAssociationsElem.AppendElem("Association"); associationElem.SetAttribute("ext", pair.Key); associationElem.SetAttribute("path", pair.Value); } ChannelNumberingOptions.SaveToXml(rootElem.AppendElem("ChannelNumberingOptions")); XmlElement customOptionsElem = rootElem.AppendElem("CustomOptions"); foreach (KeyValuePair <string, OptionList> pair in CustomOptions) { XmlElement optionGroupElem = customOptionsElem.AppendElem("OptionGroup"); optionGroupElem.SetAttribute("name", pair.Key); pair.Value.SaveToXml(optionGroupElem); } xmlDoc.Save(fileName); errMsg = ""; return(true); } catch (Exception ex) { errMsg = ex.BuildErrorMessage(CommonPhrases.SaveConfigError); return(false); } }