internal void CreateSection(SectionGroupInfo group, string name, ConfigurationSection sec) { if (group.HasChild(name)) { throw new ConfigurationErrorsException("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'"); } if (!HasFile && !sec.SectionInformation.AllowLocation) { throw new ConfigurationErrorsException("The configuration section <" + name + "> cannot be defined inside a <location> element."); } if (!system.Host.IsDefinitionAllowed(configPath, sec.SectionInformation.AllowDefinition, sec.SectionInformation.AllowExeDefinition)) { object ctx = sec.SectionInformation.AllowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object)sec.SectionInformation.AllowExeDefinition : (object)sec.SectionInformation.AllowDefinition; throw new ConfigurationErrorsException("The section <" + name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "')."); } if (sec.SectionInformation.Type == null) { sec.SectionInformation.Type = system.Host.GetConfigTypeName(sec.GetType()); } SectionInfo section = new SectionInfo(name, sec.SectionInformation); section.StreamName = streamName; section.ConfigHost = system.Host; group.AddChild(section); elementData [section] = sec; sec.Configuration = this; }
public void Remove(string name) { SectionInfo secData = group.Sections [name] as SectionInfo; if (secData != null) { config.RemoveConfigInfo(secData); } }
internal ConfigurationSection GetSectionInstance(SectionInfo config, bool createDefaultInstance) { object data = _elementData[config]; var sec = data as ConfigurationSection; if (sec != null || !createDefaultInstance) { return(sec); } var secObj = config.CreateInstance(); sec = secObj as ConfigurationSection; if (sec == null) { var ds = new DefaultSection(); ds.SectionHandler = secObj as IConfigurationSectionHandler; sec = ds; } sec.Configuration = this; ConfigurationSection parentSection = null; if (Parent != null) { parentSection = Parent.GetSectionInstance(config, true); sec.SectionInformation.SetParentSection(parentSection); } sec.SectionInformation.ConfigFilePath = FilePath; sec.ConfigContext = _system.Host.CreateDeprecatedConfigContext(_configPath); var xml = data as string; sec.RawXml = xml; sec.Reset(parentSection); if (xml != null) { using (var r = XmlReader.Create(new StringReader(xml))) { sec.DeserializeSection(r); } if (!string.IsNullOrEmpty(sec.SectionInformation.ConfigSource) && !string.IsNullOrEmpty(FilePath)) { sec.DeserializeConfigSource(Path.GetDirectoryName(FilePath)); } } _elementData[config] = sec; return(sec); }
internal ConfigurationSection GetSectionInstance(SectionInfo config, bool createDefaultInstance) { object data = elementData [config]; ConfigurationSection sec = data as ConfigurationSection; if (sec != null || !createDefaultInstance) { return(sec); } object secObj = config.CreateInstance(); sec = secObj as ConfigurationSection; if (sec == null) { DefaultSection ds = new DefaultSection(); ds.SectionHandler = secObj as IConfigurationSectionHandler; sec = ds; } sec.Configuration = this; ConfigurationSection parentSection = null; if (parent != null) { parentSection = parent.GetSectionInstance(config, true); sec.SectionInformation.SetParentSection(parentSection); } sec.SectionInformation.ConfigFilePath = FilePath; sec.ConfigContext = system.Host.CreateDeprecatedConfigContext(configPath); string xml = data as string; sec.RawXml = xml; sec.Reset(parentSection); if (xml != null) { XmlTextReader r = new ConfigXmlTextReader(new StringReader(xml), FilePath); sec.DeserializeSection(r); r.Close(); if (!String.IsNullOrEmpty(sec.SectionInformation.ConfigSource) && !String.IsNullOrEmpty(FilePath)) { sec.DeserializeConfigSource(Path.GetDirectoryName(FilePath)); } } elementData [config] = sec; return(sec); }
private SectionInfo CreateSectionInfo(IConfigSystem system, string configPath, string sectionName, string sectionType) { var sectionInformation = new SectionInformation { Type = sectionType, ConfigFilePath = configPath }; sectionInformation.SetName(sectionName); var sectionInfo = new SectionInfo(sectionName, sectionInformation) { StreamName = FileName, ConfigHost = system.Host }; return(sectionInfo); }
public ConfigurationSection this [string name] { get { ConfigurationSection sec = BaseGet(name) as ConfigurationSection; if (sec == null) { SectionInfo secData = group.Sections [name] as SectionInfo; if (secData == null) { return(null); } sec = config.GetSectionInstance(secData, true); if (sec == null) { return(null); } BaseSet(name, sec); } return(sec); } }
internal void SetSectionXml (SectionInfo config, string data) { elementData [config] = data; }
internal void SetConfigurationSection (SectionInfo config, ConfigurationSection sec) { elementData [config] = sec; }
internal ConfigurationSection GetSectionInstance (SectionInfo config, bool createDefaultInstance) { object data = elementData [config]; ConfigurationSection sec = data as ConfigurationSection; if (sec != null || !createDefaultInstance) return sec; object secObj = config.CreateInstance (); sec = secObj as ConfigurationSection; if (sec == null) { DefaultSection ds = new DefaultSection (); ds.SectionHandler = secObj as IConfigurationSectionHandler; sec = ds; } sec.Configuration = this; ConfigurationSection parentSection = null; if (parent != null) { parentSection = parent.GetSectionInstance (config, true); sec.SectionInformation.SetParentSection (parentSection); } sec.SectionInformation.ConfigFilePath = FilePath; sec.ConfigContext = system.Host.CreateDeprecatedConfigContext(configPath); string xml = data as string; sec.RawXml = xml; sec.Reset (parentSection); if (xml != null) { XmlTextReader r = new ConfigXmlTextReader (new StringReader (xml), FilePath); sec.DeserializeSection (r); r.Close (); if (!String.IsNullOrEmpty (sec.SectionInformation.ConfigSource) && !String.IsNullOrEmpty (FilePath)) sec.DeserializeConfigSource (Path.GetDirectoryName (FilePath)); } elementData [config] = sec; return sec; }
public override void ReadConfig(Configuration cfg, string streamName, XmlReader reader) { StreamName = streamName; ConfigHost = cfg.ConfigHost; if (reader.LocalName != "configSections") { while (reader.MoveToNextAttribute()) { if (reader.Name == "name") { Name = reader.Value; } else if (reader.Name == "type") { TypeName = reader.Value; Type = null; } else { ThrowException("Unrecognized attribute", reader); } } if (Name == null) { ThrowException("sectionGroup must have a 'name' attribute", reader); } if (Name == "location") { ThrowException("location is a reserved section name", reader); } } if (TypeName == null) { TypeName = "System.Configuration.ConfigurationSectionGroup"; } if (reader.IsEmptyElement) { reader.Skip(); return; } reader.ReadStartElement(); reader.MoveToContent(); while (reader.NodeType != XmlNodeType.EndElement) { if (reader.NodeType != XmlNodeType.Element) { reader.Skip(); continue; } string name = reader.LocalName; ConfigInfo cinfo = null; if (name == "remove") { ReadRemoveSection(reader); continue; } if (name == "clear") { if (reader.HasAttributes) { ThrowException("Unrecognized attribute.", reader); } Clear(); reader.Skip(); continue; } if (name == "section") { cinfo = new SectionInfo(); } else if (name == "sectionGroup") { cinfo = new SectionGroupInfo(); } else { ThrowException("Unrecognized element: " + reader.Name, reader); } cinfo.ReadConfig(cfg, streamName, reader); var actInfo = Groups[cinfo.Name]; if (actInfo == null) { actInfo = Sections[cinfo.Name]; } if (actInfo != null) { if (actInfo.GetType() != cinfo.GetType()) { ThrowException("A section or section group named '" + cinfo.Name + "' already exists", reader); } // Merge all the new data actInfo.Merge(cinfo); // Make sure that this section is saved in this configuration file: actInfo.StreamName = streamName; } else { AddChild(cinfo); } } reader.ReadEndElement(); }
internal string GetSectionXml(SectionInfo config) { return(elementData [config] as string); }
internal void SetConfigurationSection(SectionInfo config, ConfigurationSection sec) { elementData [config] = sec; }
public void RemoveAt(int index) { SectionInfo secData = group.Sections [index] as SectionInfo; config.RemoveConfigInfo(secData); }
public override void ReadConfig (Configuration cfg, string streamName, XmlReader reader) { StreamName = streamName; ConfigHost = cfg.ConfigHost; if (reader.LocalName != "configSections") { while (reader.MoveToNextAttribute ()) { if (reader.Name == "name") Name = reader.Value; else if (reader.Name == "type") { TypeName = reader.Value; Type = null; } else ThrowException ("Unrecognized attribute", reader); } if (Name == null) ThrowException ("sectionGroup must have a 'name' attribute", reader); if (Name == "location") ThrowException ("location is a reserved section name", reader); } if (TypeName == null) TypeName = "System.Configuration.ConfigurationSectionGroup"; if (reader.IsEmptyElement) { reader.Skip (); return; } reader.ReadStartElement (); reader.MoveToContent (); while (reader.NodeType != XmlNodeType.EndElement) { if (reader.NodeType != XmlNodeType.Element) { reader.Skip (); continue; } string name = reader.LocalName; ConfigInfo cinfo = null; if (name == "remove") { ReadRemoveSection (reader); continue; } if (name == "clear") { if (reader.HasAttributes) ThrowException ("Unrecognized attribute.", reader); Clear (); reader.Skip (); continue; } if (name == "section") cinfo = new SectionInfo (); else if (name == "sectionGroup") { cinfo = new SectionGroupInfo (); } else ThrowException ("Unrecognized element: " + reader.Name, reader); cinfo.ReadConfig (cfg, streamName, reader); ConfigInfo actInfo = Groups [cinfo.Name]; if (actInfo == null) actInfo = Sections [cinfo.Name]; if (actInfo != null) { if (actInfo.GetType () != cinfo.GetType ()) ThrowException ("A section or section group named '" + cinfo.Name + "' already exists", reader); // Merge all the new data actInfo.Merge (cinfo); // Make sure that this section is saved in this configuration file: actInfo.StreamName = streamName; } else AddChild (cinfo); } reader.ReadEndElement (); }
internal string GetSectionXml (SectionInfo config) { return elementData [config] as string; }
internal void CreateSection (SectionGroupInfo group, string name, ConfigurationSection sec) { if (group.HasChild (name)) throw new ConfigurationErrorsException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'"); if (!HasFile && !sec.SectionInformation.AllowLocation) throw new ConfigurationErrorsException ("The configuration section <" + name + "> cannot be defined inside a <location> element."); if (!system.Host.IsDefinitionAllowed (configPath, sec.SectionInformation.AllowDefinition, sec.SectionInformation.AllowExeDefinition)) { object ctx = sec.SectionInformation.AllowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) sec.SectionInformation.AllowExeDefinition : (object) sec.SectionInformation.AllowDefinition; throw new ConfigurationErrorsException ("The section <" + name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "')."); } if (sec.SectionInformation.Type == null) sec.SectionInformation.Type = system.Host.GetConfigTypeName (sec.GetType ()); SectionInfo section = new SectionInfo (name, sec.SectionInformation); section.StreamName = streamName; section.ConfigHost = system.Host; group.AddChild (section); elementData [section] = sec; sec.Configuration = this; }
internal void SetSectionXml(SectionInfo config, string data) { elementData [config] = data; }