public void RenameSection(string oldName, string newName) { LoadDataIfRequired(); if (m_sections.Contains(oldName)) { if (!m_sections.Contains(newName)) { ConfigSectionImpl configSection = (ConfigSectionImpl)m_sections[oldName]; configSection.Name = newName; m_sections[newName] = configSection; m_sections.Remove(oldName); m_changed = true; } else { throw new TisException("Section [{0}] already exists", newName); } } else { throw new TisException("Section [{0}] does not exist", oldName); } }
// create new section in the config file private XmlNode CreateSectionInDocument( ConfigSectionImpl configSection, XmlDocument xmlDoc) { XmlNode sectionElement = null; try { // create the section element with the specified attribute name = 'sectionName' // and save it in the config file sectionElement = xmlDoc.CreateElement(ELEMENT_SECTION); // add the name attribute XmlAttribute nameAttribute = xmlDoc.CreateAttribute(ATTR_NAME); nameAttribute.Value = configSection.Name; sectionElement.Attributes.Append(nameAttribute); // add the serialize attribute if (configSection.InnerObjectType != null) { XmlAttribute serializeAttribute = xmlDoc.CreateAttribute(ATTR_OBJECT_TYPE); serializeAttribute.Value = ReflectionUtil.GetFullTypeString(configSection.InnerObjectType); sectionElement.Attributes.Append(serializeAttribute); } // add the ObjectType attribute XmlAttribute oObjectTypeAttribute = xmlDoc.CreateAttribute(ATTR_SERIALIZATION_TYPE); oObjectTypeAttribute.Value = Enum.GetName(typeof(ConfigSerializeType), configSection.SerializeType); sectionElement.Attributes.Append(oObjectTypeAttribute); // set the inner XML data for the section sectionElement.InnerXml = configSection.InnerXml; xmlDoc.DocumentElement.AppendChild(sectionElement); // Build the sub sections with all the parameters if needed // in the inner xml of the current config section configSection.BuildSubSectionsElements(xmlDoc, false); m_changed = true; } catch (Exception e) { Log.Write( Log.Severity.ERROR, System.Reflection.MethodInfo.GetCurrentMethod(), "failed , Details : {0}", e.Message); throw new TiS.Core.TisCommon.TisException( e, "failed , Details : {0}", e.Message); } return(sectionElement); }
/// <summary> /// Create the config section in the memory map ( m_sections ) /// </summary> /// <param name="sectionName"></param> /// <returns></returns> private IConfigSection CreateSectionInMemory(string sectionName) { ConfigSectionImpl configSection = new ConfigSectionImpl( sectionName, ConfigSerializeType.None, String.Empty, string.Empty); m_sections[sectionName] = configSection; m_changed = true; return(configSection); }
/// <summary> /// Go through all the sections in memory and save them /// to the config file . /// </summary> /// <returns></returns> public void SaveAllSections() { InitSectionsTableIfRequired(); XmlDocument xmlDoc = CreateConfigDocument(); try { foreach (DictionaryEntry sectionEntry in m_sections) { ConfigSectionImpl configSection = (ConfigSectionImpl)sectionEntry.Value; CreateSectionInDocument( configSection, xmlDoc); } // Prepare a formatted XML string StringWriter oStringWriter = new StringWriter(); XmlTextWriter oXmlWriter = new XmlTextWriter(oStringWriter); oXmlWriter.Formatting = Formatting.Indented; oXmlWriter.Indentation = 3; xmlDoc.WriteTo(oXmlWriter); oXmlWriter.Flush(); oStringWriter.Flush(); string sXml = oStringWriter.GetStringBuilder().ToString(); StoreConfigXML(sXml); m_changed = false; } catch (Exception e) { Log.Write( Log.Severity.ERROR, System.Reflection.MethodInfo.GetCurrentMethod(), "failed , Details : {0}", e.Message); throw new TiS.Core.TisCommon.TisException( e, "failed , Details : {0}", e.Message); } }
// load all the config sections from the file // into m_sections table private void LoadConfigSections() { try { m_changed = false; // clear the table m_sections.Clear(); // load the config file XmlDocument xmlDoc = LoadConfigDocument(); if (xmlDoc == null) { return; } string xPath = ELEMENT_SECTION; XmlNodeList sectionList = xmlDoc.DocumentElement.SelectNodes(xPath); XmlNode sectionNode; if (sectionList != null) { for (int i = 0; i < sectionList.Count; i++) { sectionNode = sectionList.Item(i); if (sectionNode != null) { string serializeTypeName = ""; string sectionName = ""; string sInnerObjectType = string.Empty; ConfigSerializeType serializeType = ConfigSerializeType.None; if (sectionNode.Attributes[ATTR_NAME] != null) { sectionName = sectionNode.Attributes[ATTR_NAME].Value.ToString(); } else { throw new TisException("Load Config Sections failed , the Section is missing the 'name' attribute."); } if (sectionNode.Attributes[ATTR_OBJECT_TYPE] != null) { sInnerObjectType = sectionNode.Attributes[ATTR_OBJECT_TYPE].Value.ToString(); } if (sectionNode.Attributes[ATTR_SERIALIZATION_TYPE] != null) { serializeTypeName = sectionNode.Attributes[ATTR_SERIALIZATION_TYPE].Value.ToString(); object serializeTypeObj = Enum.Parse(typeof(ConfigSerializeType), serializeTypeName); serializeType = (ConfigSerializeType)serializeTypeObj; } else { throw new TisException("Load Config Sections failed , the Section {0} is missing the Attribute : {1} value", sectionName, ATTR_SERIALIZATION_TYPE); } ConfigSectionImpl configSection = new ConfigSectionImpl( sectionName, serializeType, sInnerObjectType, sectionNode.InnerXml); // build the sub sections if needed configSection.BuildSubSectionsElements(xmlDoc, true); // save the config section in the map m_sections[sectionName] = configSection; } } } } catch (Exception e) { // write error to Log File. Log.Write( Log.Severity.ERROR, System.Reflection.MethodInfo.GetCurrentMethod(), "failed , Details : {0}", e.Message); throw new TiS.Core.TisCommon.TisException( e, "failed , Details : {0}", e.Message); } }