<PackageReference Include="System.Configuration.ConfigurationManager" Version="9.0.0-preview.7.24405.7" />

MgmtConfigurationRecord

using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration.Internal; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.Versioning; using System.Text; using System.Xml; namespace System.Configuration { internal sealed class MgmtConfigurationRecord : BaseConfigurationRecord { private const int DefaultIndent = 4; private const int MaxIndent = 10; private static readonly SimpleBitVector32 s_mgmtClassFlags = new SimpleBitVector32(80); private Hashtable _locationTags; private Hashtable _removedSectionGroups; private Hashtable _removedSections; private Hashtable _sectionFactories; private Hashtable _sectionGroupFactories; private Hashtable _sectionGroups; private HybridDictionary _streamInfoUpdates; private MgmtConfigurationRecord MgmtParent => (MgmtConfigurationRecord)_parent; private UpdateConfigHost UpdateConfigHost => _configRoot.UpdateConfigHost; protected override SimpleBitVector32 ClassFlags => s_mgmtClassFlags; private Hashtable SectionGroups => _sectionGroups ?? (_sectionGroups = new Hashtable()); private Hashtable RemovedSections => _removedSections ?? (_removedSections = new Hashtable()); private Hashtable RemovedSectionGroups => _removedSectionGroups ?? (_removedSectionGroups = new Hashtable()); internal Hashtable SectionFactories => _sectionFactories ?? (_sectionFactories = GetAllFactories(false)); internal Hashtable SectionGroupFactories => _sectionGroupFactories ?? (_sectionGroupFactories = GetAllFactories(true)); internal string ConfigurationFilePath => UpdateConfigHost.GetNewStreamname(base.ConfigStreamInfo.StreamName) ?? string.Empty; private bool HasRemovedSectionsOrGroups { get { if (_removedSections == null || _removedSections.Count <= 0) { if (_removedSectionGroups != null) return _removedSectionGroups.Count > 0; return false; } return true; } } private bool HasRemovedSections { get { if (_removedSections != null) return _removedSections.Count > 0; return false; } } internal bool NamespacePresent { get { return _flags[67108864]; } set { _flags[67108864] = value; } } private NamespaceChange NamespaceChangeNeeded { get { if (_flags[67108864] == _flags[512]) return NamespaceChange.None; if (!_flags[67108864]) return NamespaceChange.Remove; return NamespaceChange.Add; } } private bool RecordItselfRequiresUpdates => NamespaceChangeNeeded != NamespaceChange.None; private MgmtConfigurationRecord() { } internal static MgmtConfigurationRecord Create(IInternalConfigRoot configRoot, IInternalConfigRecord parent, string configPath, string locationSubPath) { MgmtConfigurationRecord mgmtConfigurationRecord = new MgmtConfigurationRecord(); mgmtConfigurationRecord.Init(configRoot, parent, configPath, locationSubPath); return mgmtConfigurationRecord; } private void Init(IInternalConfigRoot configRoot, IInternalConfigRecord parent, string configPath, string locationSubPath) { base.Init(configRoot, (BaseConfigurationRecord)parent, configPath, locationSubPath); if (base.IsLocationConfig && (MgmtParent._locationTags == null || !MgmtParent._locationTags.Contains(_locationSubPath))) _flags[16777216] = true; InitStreamInfoUpdates(); } private void InitStreamInfoUpdates() { _streamInfoUpdates = new HybridDictionary(true); if (base.ConfigStreamInfo.HasStreamInfos) { foreach (StreamInfo value in base.ConfigStreamInfo.StreamInfos.Values) { _streamInfoUpdates.Add(value.StreamName, value.Clone()); } } } protected override object CreateSectionFactory(FactoryRecord factoryRecord) { Type type = TypeUtil.GetType(base.Host, factoryRecord.FactoryTypeName, true); if (!typeof(ConfigurationSection).IsAssignableFrom(type)) { TypeUtil.VerifyAssignableType(typeof(IConfigurationSectionHandler), type, true); type = typeof(DefaultSection); } return TypeUtil.GetConstructor(type, typeof(ConfigurationSection), true); } protected override object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader) { ConfigurationSection configurationSection = (ConfigurationSection)((ConstructorInfo)factoryRecord.Factory).Invoke(null); configurationSection.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord); configurationSection.CallInit(); ConfigurationSection parentElement = (ConfigurationSection)parentConfig; configurationSection.Reset(parentElement); if (reader != null) configurationSection.DeserializeSection(reader); configurationSection.ResetModified(); return configurationSection; } private ConstructorInfo CreateSectionGroupFactory(FactoryRecord factoryRecord) { return TypeUtil.GetConstructor(string.IsNullOrEmpty(factoryRecord.FactoryTypeName) ? typeof(ConfigurationSectionGroup) : TypeUtil.GetType(base.Host, factoryRecord.FactoryTypeName, true), typeof(ConfigurationSectionGroup), true); } private ConstructorInfo EnsureSectionGroupFactory(FactoryRecord factoryRecord) { ConstructorInfo constructorInfo = (ConstructorInfo)factoryRecord.Factory; if (constructorInfo == (ConstructorInfo)null) constructorInfo = (ConstructorInfo)(factoryRecord.Factory = CreateSectionGroupFactory(factoryRecord)); return constructorInfo; } protected override object UseParentResult(string configKey, object parentResult, SectionRecord sectionRecord) { FactoryRecord factoryRecord = FindFactoryRecord(configKey, false); if (factoryRecord == null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_unrecognized_configuration_section, configKey)); return CallCreateSection(false, factoryRecord, sectionRecord, parentResult, null, null, -1); } protected override object GetRuntimeObject(object result) { return result; } private ConfigurationSection GetConfigSection(string configKey) { SectionRecord sectionRecord = GetSectionRecord(configKey, false); if (sectionRecord != null && sectionRecord.HasResult) return (ConfigurationSection)sectionRecord.Result; return null; } internal ConfigurationSectionGroup LookupSectionGroup(string configKey) { ConfigurationSectionGroup result = null; if (_sectionGroups != null) result = (ConfigurationSectionGroup)_sectionGroups[configKey]; return result; } internal ConfigurationSectionGroup GetSectionGroup(string configKey) { ConfigurationSectionGroup configurationSectionGroup = LookupSectionGroup(configKey); if (configurationSectionGroup != null) return configurationSectionGroup; BaseConfigurationRecord configRecord; FactoryRecord factoryRecord = FindFactoryRecord(configKey, false, out configRecord); if (factoryRecord == null) return null; if (!factoryRecord.IsGroup) throw ExceptionUtil.ParameterInvalid("sectionGroupName"); if (factoryRecord.FactoryTypeName == null) configurationSectionGroup = new ConfigurationSectionGroup(); else { ConstructorInfo constructorInfo = EnsureSectionGroupFactory(factoryRecord); try { configurationSectionGroup = (ConfigurationSectionGroup)constructorInfo.Invoke(null); } catch (Exception inner) { throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_exception_creating_section_handler, factoryRecord.ConfigKey), inner, factoryRecord); } } configurationSectionGroup.AttachToConfigurationRecord(this, factoryRecord); SectionGroups[configKey] = configurationSectionGroup; return configurationSectionGroup; } internal ConfigurationLocationCollection GetLocationCollection(Configuration config) { ArrayList arrayList = new ArrayList(); if (_locationTags != null) { foreach (string value in _locationTags.Values) { arrayList.Add(new ConfigurationLocation(config, value)); } } return new ConfigurationLocationCollection(arrayList); } protected override void AddLocation(string locationSubPath) { if (_locationTags == null) _locationTags = new Hashtable(StringComparer.OrdinalIgnoreCase); _locationTags[locationSubPath] = locationSubPath; } private Hashtable GetAllFactories(bool isGroup) { Hashtable hashtable = new Hashtable(); MgmtConfigurationRecord mgmtConfigurationRecord = this; do { if (mgmtConfigurationRecord._factoryRecords != null) { foreach (FactoryRecord value in mgmtConfigurationRecord._factoryRecords.Values) { if (value.IsGroup == isGroup) { string configKey = value.ConfigKey; hashtable[configKey] = new FactoryId(value.ConfigKey, value.Group, value.Name); } } } mgmtConfigurationRecord = mgmtConfigurationRecord.MgmtParent; } while (!mgmtConfigurationRecord.IsRootConfig); return hashtable; } internal ConfigurationSection FindImmediateParentSection(ConfigurationSection section) { ConfigurationSection configurationSection = null; string sectionName = section.SectionInformation.SectionName; SectionRecord sectionRecord = GetSectionRecord(sectionName, false); if (sectionRecord.HasLocationInputs) configurationSection = (ConfigurationSection)sectionRecord.LastLocationInput.Result; else if (sectionRecord.HasIndirectLocationInputs) { configurationSection = (ConfigurationSection)sectionRecord.LastIndirectLocationInput.Result; } else if (IsRootDeclaration(sectionName, true)) { FactoryRecord factoryRecord = GetFactoryRecord(sectionName, false); CreateSectionDefault(sectionName, false, factoryRecord, null, out object result, out object _); configurationSection = (ConfigurationSection)result; } else { MgmtConfigurationRecord mgmtParent = MgmtParent; while (!mgmtParent.IsRootConfig) { sectionRecord = mgmtParent.GetSectionRecord(sectionName, false); if (sectionRecord != null && sectionRecord.HasResult) { configurationSection = (ConfigurationSection)sectionRecord.Result; break; } mgmtParent = mgmtParent.MgmtParent; } } if (!configurationSection.IsReadOnly()) configurationSection.SetReadOnly(); return configurationSection; } internal ConfigurationSection FindAndCloneImmediateParentSection(ConfigurationSection configSection) { string configKey = configSection.SectionInformation.ConfigKey; ConfigurationSection parentResult = FindImmediateParentSection(configSection); SectionRecord sectionRecord = GetSectionRecord(configKey, false); return (ConfigurationSection)UseParentResult(configKey, parentResult, sectionRecord); } internal void RevertToParent(ConfigurationSection configSection) { configSection.SectionInformation.RawXml = null; try { ConfigurationSection parentElement = FindImmediateParentSection(configSection); configSection.Reset(parentElement); configSection.ResetModified(); } catch (Exception inner) { throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName), inner, base.ConfigStreamInfo.StreamName, 0); } configSection.SectionInformation.Removed = true; } internal string GetRawXml(string configKey) { SectionRecord sectionRecord = GetSectionRecord(configKey, false); if (sectionRecord == null || !sectionRecord.HasFileInput) return null; string[] keys = configKey.Split(BaseConfigurationRecord.s_configPathSeparatorParams); return GetSectionXmlReader(keys, sectionRecord.FileInput).RawXml; } internal void SetRawXml(ConfigurationSection configSection, string xmlElement) { if (string.IsNullOrEmpty(xmlElement)) RevertToParent(configSection); else { ValidateSectionXml(xmlElement, configSection.SectionInformation.Name); ConfigurationSection parentElement = FindImmediateParentSection(configSection); ConfigXmlReader reader = new ConfigXmlReader(xmlElement, null, 0); configSection.SectionInformation.RawXml = xmlElement; try { try { bool elementPresent = configSection.ElementPresent; PropertySourceInfo sourceInformation = configSection.ElementInformation.PropertyInfoInternal(); configSection.Reset(parentElement); configSection.DeserializeSection(reader); configSection.ResetModified(); configSection.ElementPresent = elementPresent; configSection.ElementInformation.ChangeSourceAndLineNumber(sourceInformation); } catch { configSection.SectionInformation.RawXml = null; throw; } } catch (Exception inner) { throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName), inner, null, 0); } configSection.SectionInformation.Removed = false; } } private bool IsStreamUsed(string oldStreamName) { MgmtConfigurationRecord mgmtConfigurationRecord = this; if (base.IsLocationConfig) { mgmtConfigurationRecord = MgmtParent; if (mgmtConfigurationRecord._sectionRecords != null) { foreach (SectionRecord value in mgmtConfigurationRecord._sectionRecords.Values) { if (value.HasFileInput && StringUtil.EqualsIgnoreCase(value.FileInput.SectionXmlInfo.ConfigSourceStreamName, oldStreamName)) return true; } } } if (mgmtConfigurationRecord._locationSections == null) return false; foreach (LocationSectionRecord locationSection in mgmtConfigurationRecord._locationSections) { if (StringUtil.EqualsIgnoreCase(locationSection.SectionXmlInfo.ConfigSourceStreamName, oldStreamName)) return true; } return false; } internal void ChangeConfigSource(SectionInformation sectionInformation, string oldConfigSource, string oldConfigSourceStreamName, string newConfigSource) { if (string.IsNullOrEmpty(oldConfigSource)) oldConfigSource = null; if (string.IsNullOrEmpty(newConfigSource)) newConfigSource = null; if (!StringUtil.EqualsIgnoreCase(oldConfigSource, newConfigSource)) { if (string.IsNullOrEmpty(base.ConfigStreamInfo.StreamName)) throw new ConfigurationErrorsException(System.SR.Config_source_requires_file); string text = null; if (newConfigSource != null) text = base.Host.GetStreamNameForConfigSource(base.ConfigStreamInfo.StreamName, newConfigSource); if (text != null) { ValidateUniqueChildConfigSource(text, newConfigSource, null); StreamInfo streamInfo = (StreamInfo)_streamInfoUpdates[text]; if (streamInfo != null) { if (streamInfo.SectionName != sectionInformation.ConfigKey) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_source_cannot_be_shared, newConfigSource)); } else { streamInfo = new StreamInfo(sectionInformation.ConfigKey, newConfigSource, text); _streamInfoUpdates.Add(text, streamInfo); } } if (oldConfigSourceStreamName != null && !IsStreamUsed(oldConfigSourceStreamName)) _streamInfoUpdates.Remove(oldConfigSourceStreamName); sectionInformation.ConfigSourceStreamName = text; } } private static void ValidateSectionXml(string xmlElement, string configKey) { if (!string.IsNullOrEmpty(xmlElement)) { XmlTextReader xmlTextReader = null; try { XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode); xmlTextReader = new XmlTextReader(xmlElement, XmlNodeType.Element, context); xmlTextReader.Read(); if (xmlTextReader.NodeType != XmlNodeType.Element) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_unexpected_node_type, xmlTextReader.NodeType)); BaseConfigurationRecord.SplitConfigKey(configKey, out string _, out string name); if (xmlTextReader.Name != name) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_unexpected_element_name, xmlTextReader.Name)); do { if (!xmlTextReader.Read()) { if (xmlTextReader.Depth != 0) throw new ConfigurationErrorsException(System.SR.Config_unexpected_element_end, xmlTextReader); return; } XmlNodeType nodeType = xmlTextReader.NodeType; if (nodeType == XmlNodeType.DocumentType || nodeType == XmlNodeType.XmlDeclaration) throw new ConfigurationErrorsException(System.SR.Config_invalid_node_type, xmlTextReader); } while (xmlTextReader.Depth > 0 || xmlTextReader.NodeType == XmlNodeType.EndElement); throw new ConfigurationErrorsException(System.SR.Config_more_data_than_expected, xmlTextReader); } finally { xmlTextReader?.Close(); } } } internal void AddConfigurationSection(string group, string name, ConfigurationSection configSection) { if (base.IsLocationConfig) throw new InvalidOperationException(System.SR.Config_add_configurationsection_in_location_config); BaseConfigurationRecord.VerifySectionName(name, null, false); if (configSection == null) throw new ArgumentNullException("configSection"); if (configSection.SectionInformation.Attached) throw new InvalidOperationException(System.SR.Config_add_configurationsection_already_added); string text = BaseConfigurationRecord.CombineConfigKey(group, name); FactoryRecord factoryRecord = FindFactoryRecord(text, true); if (factoryRecord != null) throw new ArgumentException(System.SR.Config_add_configurationsection_already_exists); if (!string.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)) ChangeConfigSource(configSection.SectionInformation, null, null, configSection.SectionInformation.ConfigSource); _sectionFactories?.Add(text, new FactoryId(text, group, name)); string factoryTypeName = configSection.SectionInformation.Type ?? base.Host.GetConfigTypeName(configSection.GetType()); factoryRecord = new FactoryRecord(text, group, name, factoryTypeName, configSection.SectionInformation.AllowLocation, configSection.SectionInformation.AllowDefinition, configSection.SectionInformation.AllowExeDefinition, configSection.SectionInformation.OverrideModeDefaultSetting, configSection.SectionInformation.RestartOnExternalChanges, configSection.SectionInformation.RequirePermission, _flags[8192], false, base.ConfigStreamInfo.StreamName, -1) { Factory = TypeUtil.GetConstructor(configSection.GetType(), typeof(ConfigurationSection), true) }; EnsureFactories()[text] = factoryRecord; SectionRecord sectionRecord = EnsureSectionRecordUnsafe(text, false); sectionRecord.Result = configSection; sectionRecord.ResultRuntimeObject = configSection; _removedSections?.Remove(text); configSection.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord); string rawXml = configSection.SectionInformation.RawXml; if (!string.IsNullOrEmpty(rawXml)) { configSection.SectionInformation.RawXml = null; configSection.SectionInformation.SetRawXml(rawXml); } } internal void RemoveConfigurationSection(string group, string name) { bool flag = false; BaseConfigurationRecord.VerifySectionName(name, null, true); string text = BaseConfigurationRecord.CombineConfigKey(group, name); if (!RemovedSections.Contains(text) && FindFactoryRecord(text, true) != null) { GetConfigSection(text)?.SectionInformation.DetachFromConfigurationRecord(); bool flag2 = IsRootDeclaration(text, false); if ((_sectionFactories != null) & flag2) _sectionFactories.Remove(text); if (!base.IsLocationConfig && _factoryRecords != null && _factoryRecords.Contains(text)) { flag = true; _factoryRecords.Remove(text); } if (_sectionRecords != null && _sectionRecords.Contains(text)) { flag = true; _sectionRecords.Remove(text); } if (_locationSections != null) { int num = 0; while (num < _locationSections.Count) { if (((LocationSectionRecord)_locationSections[num]).ConfigKey != text) num++; else { flag = true; _locationSections.RemoveAt(num); } } } if (flag) RemovedSections.Add(text, text); List<string> list = new List<string>(); foreach (StreamInfo value in _streamInfoUpdates.Values) { if (value.SectionName == text) list.Add(value.StreamName); } foreach (string item in list) { _streamInfoUpdates.Remove(item); } } } internal void AddConfigurationSectionGroup(string group, string name, ConfigurationSectionGroup configSectionGroup) { if (base.IsLocationConfig) throw new InvalidOperationException(System.SR.Config_add_configurationsectiongroup_in_location_config); BaseConfigurationRecord.VerifySectionName(name, null, false); if (configSectionGroup == null) throw ExceptionUtil.ParameterInvalid("name"); if (configSectionGroup.Attached) throw new InvalidOperationException(System.SR.Config_add_configurationsectiongroup_already_added); string text = BaseConfigurationRecord.CombineConfigKey(group, name); FactoryRecord factoryRecord = FindFactoryRecord(text, true); if (factoryRecord != null) throw new ArgumentException(System.SR.Config_add_configurationsectiongroup_already_exists); _sectionGroupFactories?.Add(text, new FactoryId(text, group, name)); string factoryTypeName = configSectionGroup.Type ?? base.Host.GetConfigTypeName(configSectionGroup.GetType()); factoryRecord = new FactoryRecord(text, group, name, factoryTypeName, base.ConfigStreamInfo.StreamName, -1); EnsureFactories()[text] = factoryRecord; SectionGroups[text] = configSectionGroup; _removedSectionGroups?.Remove(text); configSectionGroup.AttachToConfigurationRecord(this, factoryRecord); } private ArrayList GetDescendentSectionFactories(string configKey) { ArrayList arrayList = new ArrayList(); string s = (configKey.Length != 0) ? (configKey + "/") : string.Empty; foreach (FactoryId value in SectionFactories.Values) { if (value.Group == configKey || StringUtil.StartsWithOrdinal(value.Group, s)) arrayList.Add(value); } return arrayList; } private ArrayList GetDescendentSectionGroupFactories(string configKey) { ArrayList arrayList = new ArrayList(); string s = (configKey.Length != 0) ? (configKey + "/") : string.Empty; foreach (FactoryId value in SectionGroupFactories.Values) { if (value.ConfigKey == configKey || StringUtil.StartsWithOrdinal(value.ConfigKey, s)) arrayList.Add(value); } return arrayList; } internal void RemoveConfigurationSectionGroup(string group, string name) { BaseConfigurationRecord.VerifySectionName(name, null, false); string configKey = BaseConfigurationRecord.CombineConfigKey(group, name); if (FindFactoryRecord(configKey, true) != null) { foreach (FactoryId descendentSectionFactory in GetDescendentSectionFactories(configKey)) { RemoveConfigurationSection(descendentSectionFactory.Group, descendentSectionFactory.Name); } foreach (FactoryId descendentSectionGroupFactory in GetDescendentSectionGroupFactories(configKey)) { if (!RemovedSectionGroups.Contains(descendentSectionGroupFactory.ConfigKey)) { LookupSectionGroup(descendentSectionGroupFactory.ConfigKey)?.DetachFromConfigurationRecord(); bool flag = IsRootDeclaration(descendentSectionGroupFactory.ConfigKey, false); if ((_sectionGroupFactories != null) & flag) _sectionGroupFactories.Remove(descendentSectionGroupFactory.ConfigKey); if (!base.IsLocationConfig) _factoryRecords?.Remove(descendentSectionGroupFactory.ConfigKey); _sectionGroups?.Remove(descendentSectionGroupFactory.ConfigKey); RemovedSectionGroups.Add(descendentSectionGroupFactory.ConfigKey, descendentSectionGroupFactory.ConfigKey); } } } } internal void SaveAs(string filename, ConfigurationSaveMode saveMode, bool forceUpdateAll) { SectionUpdates configDeclarationUpdates = GetConfigDeclarationUpdates(saveMode); bool flag = false; bool flag2 = filename != null; GetConfigDefinitionUpdates(flag2, saveMode, forceUpdateAll, out ConfigDefinitionUpdates definitionUpdates, out ArrayList configSourceUpdates); if (filename != null) { if (!base.Host.IsRemote && _streamInfoUpdates.Contains(filename)) throw new ArgumentException(System.SR.Format(System.SR.Filename_in_SaveAs_is_used_already, filename)); if (string.IsNullOrEmpty(base.ConfigStreamInfo.StreamName)) { StreamInfo value = new StreamInfo(null, null, filename); _streamInfoUpdates.Add(filename, value); base.ConfigStreamInfo.StreamName = filename; base.ConfigStreamInfo.StreamVersion = MonitorStream(null, null, base.ConfigStreamInfo.StreamName); } UpdateConfigHost.AddStreamname(base.ConfigStreamInfo.StreamName, filename, base.Host.IsRemote); foreach (StreamInfo value2 in _streamInfoUpdates.Values) { if (!string.IsNullOrEmpty(value2.SectionName)) { string newStreamname = InternalConfigHost.StaticGetStreamNameForConfigSource(filename, value2.ConfigSource); UpdateConfigHost.AddStreamname(value2.StreamName, newStreamname, base.Host.IsRemote); } } } if (!flag2) flag2 = RecordItselfRequiresUpdates; if ((configDeclarationUpdates != null || definitionUpdates != null) | flag2) { byte[] buffer = null; Encoding encoding = null; if (base.ConfigStreamInfo.HasStream) { using (Stream stream = base.Host.OpenStreamForRead(base.ConfigStreamInfo.StreamName)) { if (stream == null) throw new ConfigurationErrorsException(System.SR.Config_file_has_changed, base.ConfigStreamInfo.StreamName, 0); buffer = new byte[stream.Length]; if (stream.Read(buffer, 0, (int)stream.Length) != stream.Length) throw new ConfigurationErrorsException(System.SR.Config_data_read_count_mismatch); } try { using (StreamReader streamReader = new StreamReader(base.ConfigStreamInfo.StreamName)) { if (streamReader.Peek() >= 0) streamReader.Read(); if (streamReader.CurrentEncoding is UnicodeEncoding) encoding = streamReader.CurrentEncoding; } } catch { } } string text = FindChangedConfigurationStream(); if (text != null) throw new ConfigurationErrorsException(System.SR.Config_file_has_changed, text, 0); flag = true; object writeContext = null; bool flag3 = false; try { try { using (Stream stream2 = base.Host.OpenStreamForWrite(base.ConfigStreamInfo.StreamName, null, ref writeContext)) { flag3 = true; using (StreamWriter writer = (encoding == null) ? new StreamWriter(stream2) : new StreamWriter(stream2, encoding)) { XmlUtilWriter utilWriter = new XmlUtilWriter(writer, true); if (base.ConfigStreamInfo.HasStream) CopyConfig(configDeclarationUpdates, definitionUpdates, buffer, base.ConfigStreamInfo.StreamName, NamespaceChangeNeeded, utilWriter); else CreateNewConfig(configDeclarationUpdates, definitionUpdates, NamespaceChangeNeeded, utilWriter); } } } catch { if (flag3) base.Host.WriteCompleted(base.ConfigStreamInfo.StreamName, false, writeContext); throw; } } catch (Exception e) { throw ExceptionUtil.WrapAsConfigException(System.SR.Config_error_loading_XML_file, e, base.ConfigStreamInfo.StreamName, 0); } base.Host.WriteCompleted(base.ConfigStreamInfo.StreamName, true, writeContext); base.ConfigStreamInfo.HasStream = true; base.ConfigStreamInfo.ClearStreamInfos(); base.ConfigStreamInfo.StreamVersion = MonitorStream(null, null, base.ConfigStreamInfo.StreamName); } if (configSourceUpdates != null) { if (!flag) { string text2 = FindChangedConfigurationStream(); if (text2 != null) throw new ConfigurationErrorsException(System.SR.Config_file_has_changed, text2, 0); } foreach (DefinitionUpdate item in configSourceUpdates) { SaveConfigSource(item); } } UpdateRecords(); } private static bool AreDeclarationAttributesModified(FactoryRecord factoryRecord, ConfigurationSection configSection) { if (!(factoryRecord.FactoryTypeName != configSection.SectionInformation.Type) && factoryRecord.AllowLocation == configSection.SectionInformation.AllowLocation && factoryRecord.RestartOnExternalChanges == configSection.SectionInformation.RestartOnExternalChanges && factoryRecord.RequirePermission == configSection.SectionInformation.RequirePermission && factoryRecord.AllowDefinition == configSection.SectionInformation.AllowDefinition && factoryRecord.AllowExeDefinition == configSection.SectionInformation.AllowExeDefinition) { OverrideModeSetting overrideModeSetting = factoryRecord.OverrideModeDefault; OverrideMode overrideMode = overrideModeSetting.OverrideMode; overrideModeSetting = configSection.SectionInformation.OverrideModeDefaultSetting; if (overrideMode == overrideModeSetting.OverrideMode) return configSection.SectionInformation.IsModifiedFlags(); } return true; } private static void AppendAttribute(StringBuilder sb, string key, string value) { sb.Append(key); sb.Append("=\""); sb.Append(value); sb.Append("\" "); } private string GetUpdatedSectionDeclarationXml(FactoryRecord factoryRecord, ConfigurationSection configSection, ConfigurationSaveMode saveMode) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append('<'); stringBuilder.Append("section"); stringBuilder.Append(' '); string text = configSection.SectionInformation.Type ?? factoryRecord.FactoryTypeName; if (base.TypeStringTransformerIsSet) text = base.TypeStringTransformer(text); AppendAttribute(stringBuilder, "name", configSection.SectionInformation.Name); AppendAttribute(stringBuilder, "type", text); if (configSection.SectionInformation.AllowLocation) { switch (saveMode) { case ConfigurationSaveMode.Modified: if (configSection.SectionInformation.AllowLocationModified) break; goto IL_00b6; case ConfigurationSaveMode.Full: break; default: goto IL_00b6; } } AppendAttribute(stringBuilder, "allowLocation", configSection.SectionInformation.AllowLocation ? "true" : "false"); goto IL_00b6; IL_0133: if (configSection.SectionInformation.AllowExeDefinition == ConfigurationAllowExeDefinition.MachineToApplication) { switch (saveMode) { case ConfigurationSaveMode.Modified: if (configSection.SectionInformation.AllowExeDefinitionModified) break; goto IL_0171; case ConfigurationSaveMode.Full: break; default: goto IL_0171; } } AppendAttribute(stringBuilder, "allowExeDefinition", ExeDefinitionToString(configSection.SectionInformation.AllowExeDefinition)); goto IL_0171; IL_00b6: if (configSection.SectionInformation.AllowDefinition == ConfigurationAllowDefinition.Everywhere) { switch (saveMode) { case ConfigurationSaveMode.Modified: if (configSection.SectionInformation.AllowDefinitionModified) break; goto IL_0133; case ConfigurationSaveMode.Full: break; default: goto IL_0133; } } string value = null; switch (configSection.SectionInformation.AllowDefinition) { case ConfigurationAllowDefinition.Everywhere: value = "Everywhere"; break; case ConfigurationAllowDefinition.MachineOnly: value = "MachineOnly"; break; case ConfigurationAllowDefinition.MachineToWebRoot: value = "MachineToWebRoot"; break; case ConfigurationAllowDefinition.MachineToApplication: value = "MachineToApplication"; break; } AppendAttribute(stringBuilder, "allowDefinition", value); goto IL_0133; IL_0171: OverrideModeSetting overrideModeDefaultSetting = configSection.SectionInformation.OverrideModeDefaultSetting; if (overrideModeDefaultSetting.IsDefaultForSection) { switch (saveMode) { case ConfigurationSaveMode.Modified: if (configSection.SectionInformation.OverrideModeDefaultModified) break; goto IL_01ba; case ConfigurationSaveMode.Full: break; default: goto IL_01ba; } } StringBuilder sb = stringBuilder; overrideModeDefaultSetting = configSection.SectionInformation.OverrideModeDefaultSetting; AppendAttribute(sb, "overrideModeDefault", overrideModeDefaultSetting.OverrideModeXmlValue); goto IL_01ba; IL_01ba: if (configSection.SectionInformation.RestartOnExternalChanges) { switch (saveMode) { case ConfigurationSaveMode.Modified: if (!configSection.SectionInformation.RestartOnExternalChangesModified) break; goto case ConfigurationSaveMode.Full; case ConfigurationSaveMode.Full: AppendAttribute(stringBuilder, "restartOnExternalChanges", "true"); break; } } else AppendAttribute(stringBuilder, "restartOnExternalChanges", "false"); if (configSection.SectionInformation.RequirePermission) { switch (saveMode) { case ConfigurationSaveMode.Modified: if (!configSection.SectionInformation.RequirePermissionModified) break; goto case ConfigurationSaveMode.Full; case ConfigurationSaveMode.Full: AppendAttribute(stringBuilder, "requirePermission", "true"); break; } } else AppendAttribute(stringBuilder, "requirePermission", "false"); stringBuilder.Append("/>"); return stringBuilder.ToString(); } private static string ExeDefinitionToString(ConfigurationAllowExeDefinition allowDefinition) { switch (allowDefinition) { case ConfigurationAllowExeDefinition.MachineOnly: return "MachineOnly"; case ConfigurationAllowExeDefinition.MachineToApplication: return "MachineToApplication"; case ConfigurationAllowExeDefinition.MachineToRoamingUser: return "MachineToRoamingUser"; case ConfigurationAllowExeDefinition.MachineToLocalUser: return "MachineToLocalUser"; default: throw ExceptionUtil.PropertyInvalid("AllowExeDefinition"); } } private string GetUpdatedSectionGroupDeclarationXml(FactoryRecord factoryRecord, ConfigurationSectionGroup configSectionGroup) { if (base.TargetFramework != (FrameworkName)null && !configSectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework)) return null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append('<'); stringBuilder.Append("sectionGroup"); stringBuilder.Append(' '); AppendAttribute(stringBuilder, "name", configSectionGroup.Name); string text = configSectionGroup.Type ?? factoryRecord.FactoryTypeName; if (base.TypeStringTransformerIsSet) text = base.TypeStringTransformer(text); AppendAttribute(stringBuilder, "type", text); stringBuilder.Append('>'); return stringBuilder.ToString(); } private SectionUpdates GetConfigDeclarationUpdates(ConfigurationSaveMode saveMode) { if (base.IsLocationConfig) return null; bool flag = HasRemovedSectionsOrGroups; SectionUpdates sectionUpdates = new SectionUpdates(string.Empty); if (_factoryRecords != null) { foreach (FactoryRecord value in _factoryRecords.Values) { if (!value.IsGroup) { string text = null; if (!value.IsUndeclared) { ConfigurationSection configSection = GetConfigSection(value.ConfigKey); if (configSection != null) { if (!configSection.SectionInformation.IsDeclared && !MgmtParent.IsRootConfig && MgmtParent.FindFactoryRecord(value.ConfigKey, false) != null) { if (value.HasFile) flag = true; continue; } if (base.TargetFramework != (FrameworkName)null && !configSection.ShouldSerializeSectionInTargetVersion(base.TargetFramework)) continue; if (AreDeclarationAttributesModified(value, configSection) || !value.HasFile) { text = GetUpdatedSectionDeclarationXml(value, configSection, saveMode); if (!string.IsNullOrEmpty(text)) flag = true; } } DeclarationUpdate update = new DeclarationUpdate(value.ConfigKey, !value.HasFile, text); sectionUpdates.AddSection(update); } } else { bool flag2 = false; ConfigurationSectionGroup configurationSectionGroup = LookupSectionGroup(value.ConfigKey); if (!value.HasFile) flag2 = true; else if (configurationSectionGroup != null && configurationSectionGroup.IsDeclarationRequired) { flag2 = true; } else if (value.FactoryTypeName != null || configurationSectionGroup != null) { FactoryRecord factoryRecord2 = null; if (!MgmtParent.IsRootConfig) factoryRecord2 = MgmtParent.FindFactoryRecord(value.ConfigKey, false); flag2 = (factoryRecord2?.FactoryTypeName == null); } if (flag2) { string text2 = null; if (!value.HasFile || (configurationSectionGroup != null && configurationSectionGroup.Type != value.FactoryTypeName)) { text2 = GetUpdatedSectionGroupDeclarationXml(value, configurationSectionGroup); if (!string.IsNullOrEmpty(text2)) flag = true; } DeclarationUpdate update2 = new DeclarationUpdate(value.ConfigKey, !value.HasFile, text2); sectionUpdates.AddSectionGroup(update2); } } } } if (_sectionRecords != null) { foreach (SectionRecord value2 in _sectionRecords.Values) { if (GetFactoryRecord(value2.ConfigKey, false) == null && value2.HasResult) { ConfigurationSection configurationSection = (ConfigurationSection)value2.Result; FactoryRecord factoryRecord3 = MgmtParent.FindFactoryRecord(value2.ConfigKey, false); if (configurationSection.SectionInformation.IsDeclared) { string updatedSectionDeclarationXml = GetUpdatedSectionDeclarationXml(factoryRecord3, configurationSection, saveMode); if (!string.IsNullOrEmpty(updatedSectionDeclarationXml)) { flag = true; DeclarationUpdate update3 = new DeclarationUpdate(factoryRecord3.ConfigKey, true, updatedSectionDeclarationXml); sectionUpdates.AddSection(update3); } } } } } if (_sectionGroups != null) { foreach (ConfigurationSectionGroup value3 in _sectionGroups.Values) { if (GetFactoryRecord(value3.SectionGroupName, false) == null) { FactoryRecord factoryRecord4 = MgmtParent.FindFactoryRecord(value3.SectionGroupName, false); if (value3.IsDeclared || (factoryRecord4 != null && value3.Type != factoryRecord4.FactoryTypeName)) { string updatedSectionGroupDeclarationXml = GetUpdatedSectionGroupDeclarationXml(factoryRecord4, value3); if (!string.IsNullOrEmpty(updatedSectionGroupDeclarationXml)) { flag = true; DeclarationUpdate update4 = new DeclarationUpdate(factoryRecord4.ConfigKey, true, updatedSectionGroupDeclarationXml); sectionUpdates.AddSectionGroup(update4); } } } } } if (flag) return sectionUpdates; return null; } private static bool AreLocationAttributesModified(SectionRecord sectionRecord, ConfigurationSection configSection) { OverrideModeSetting x = OverrideModeSetting.s_locationDefault; bool flag = true; if (sectionRecord.HasFileInput) { SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo; x = sectionXmlInfo.OverrideModeSetting; flag = !sectionXmlInfo.SkipInChildApps; } if (OverrideModeSetting.CanUseSameLocationTag(x, configSection.SectionInformation.OverrideModeSetting)) return flag != configSection.SectionInformation.InheritInChildApplications; return true; } private static bool AreSectionAttributesModified(SectionRecord sectionRecord, ConfigurationSection configSection) { string s; string s2; if (sectionRecord.HasFileInput) { SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo; s = sectionXmlInfo.ConfigSource; s2 = sectionXmlInfo.ProtectionProviderName; } else { s = null; s2 = null; } if (StringUtil.EqualsOrBothNullOrEmpty(s, configSection.SectionInformation.ConfigSource) && StringUtil.EqualsOrBothNullOrEmpty(s2, configSection.SectionInformation.ProtectionProviderName)) return AreLocationAttributesModified(sectionRecord, configSection); return true; } private static bool IsConfigSectionMoved(SectionRecord sectionRecord, ConfigurationSection configSection) { if (!sectionRecord.HasFileInput) return true; return AreLocationAttributesModified(sectionRecord, configSection); } private void GetConfigDefinitionUpdates(bool requireUpdates, ConfigurationSaveMode saveMode, bool forceSaveAll, out ConfigDefinitionUpdates definitionUpdates, out ArrayList configSourceUpdates) { definitionUpdates = new ConfigDefinitionUpdates(); configSourceUpdates = null; bool flag = HasRemovedSections; if (_sectionRecords != null) { InitProtectedConfigurationSection(); IDictionaryEnumerator enumerator = _sectionRecords.GetEnumerator(); try { while (enumerator.MoveNext()) { DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current; string text = (string)dictionaryEntry.Key; SectionRecord sectionRecord = (SectionRecord)dictionaryEntry.Value; sectionRecord.AddUpdate = false; bool flag2 = sectionRecord.HasFileInput; OverrideModeSetting overrideMode = OverrideModeSetting.s_locationDefault; bool flag3 = true; bool flag4 = false; string text2 = null; bool flag5 = false; if (!sectionRecord.HasResult) { if (sectionRecord.HasFileInput) { SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo; overrideMode = sectionXmlInfo.OverrideModeSetting; flag3 = !sectionXmlInfo.SkipInChildApps; flag5 = (requireUpdates && !string.IsNullOrEmpty(sectionXmlInfo.ConfigSource)); } } else { ConfigurationSection configurationSection = (ConfigurationSection)sectionRecord.Result; if (base.TargetFramework != (FrameworkName)null && !configurationSection.ShouldSerializeSectionInTargetVersion(base.TargetFramework)) continue; overrideMode = configurationSection.SectionInformation.OverrideModeSetting; flag3 = configurationSection.SectionInformation.InheritInChildApplications; if (!configurationSection.SectionInformation.AllowLocation && (!overrideMode.IsDefaultForLocationTag || !flag3)) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_inconsistent_location_attributes, text)); flag5 = (requireUpdates && !string.IsNullOrEmpty(configurationSection.SectionInformation.ConfigSource)); try { bool flag6 = configurationSection.SectionInformation.ForceSave || configurationSection.IsModified() || (forceSaveAll && !configurationSection.SectionInformation.IsLocked); bool flag7 = AreSectionAttributesModified(sectionRecord, configurationSection); bool flag8 = flag6 || configurationSection.SectionInformation.RawXml != null; if (flag8 | flag7) { configurationSection.SectionInformation.VerifyIsEditable(); configurationSection.SectionInformation.Removed = false; flag2 = true; flag4 = IsConfigSectionMoved(sectionRecord, configurationSection); if (!flag5) flag5 = (!string.IsNullOrEmpty(configurationSection.SectionInformation.ConfigSource) && (flag8 || configurationSection.SectionInformation.ConfigSourceModified)); if (flag6 || configurationSection.SectionInformation.RawXml == null || saveMode == ConfigurationSaveMode.Full) { ConfigurationSection parentElement = FindImmediateParentSection(configurationSection); text2 = configurationSection.SerializeSection(parentElement, configurationSection.SectionInformation.Name, saveMode); ValidateSectionXml(text2, text); } else text2 = configurationSection.SectionInformation.RawXml; if (string.IsNullOrEmpty(text2) && (!string.IsNullOrEmpty(configurationSection.SectionInformation.ConfigSource) || !configurationSection.SectionInformation.LocationAttributesAreDefault || configurationSection.SectionInformation.ProtectionProvider != null)) text2 = WriteEmptyElement(configurationSection.SectionInformation.Name); if (string.IsNullOrEmpty(text2)) { configurationSection.SectionInformation.Removed = true; text2 = null; flag2 = false; if (sectionRecord.HasFileInput) { flag = true; sectionRecord.RemoveFileInput(); } } else { if ((flag7 | flag4) || string.IsNullOrEmpty(configurationSection.SectionInformation.ConfigSource)) flag = true; if (configurationSection.SectionInformation.ProtectionProvider != null) { ProtectedConfigurationSection protectedConfigSection = GetSection("configProtectedData") as ProtectedConfigurationSection; try { text2 = ProtectedConfigurationSection.FormatEncryptedSection(base.Host.EncryptSection(text2, configurationSection.SectionInformation.ProtectionProvider, protectedConfigSection), configurationSection.SectionInformation.Name, configurationSection.SectionInformation.ProtectionProvider.Name); } catch (Exception ex) { throw new ConfigurationErrorsException(System.SR.Format(System.SR.Encryption_failed, configurationSection.SectionInformation.SectionName, configurationSection.SectionInformation.ProtectionProvider.Name, ex.Message), ex); } } } } else if (configurationSection.SectionInformation.Removed) { flag2 = false; if (sectionRecord.HasFileInput) flag = true; } } catch (Exception inner) { throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_exception_in_config_section_handler, configurationSection.SectionInformation.SectionName), inner); } } if (flag2) { if (GetSectionLockedMode(sectionRecord.ConfigKey) == OverrideMode.Deny) throw new ConfigurationErrorsException(System.SR.Config_section_locked, (IConfigErrorInfo)null); sectionRecord.AddUpdate = true; DefinitionUpdate value = definitionUpdates.AddUpdate(overrideMode, flag3, flag4, text2, sectionRecord); if (flag5) { if (configSourceUpdates == null) configSourceUpdates = new ArrayList(); configSourceUpdates.Add(value); } } } } finally { (enumerator as IDisposable)?.Dispose(); } } if (_flags[16777216]) { flag = true; definitionUpdates.RequireLocation = true; } if (_flags[33554432]) flag = true; if (flag) definitionUpdates.CompleteUpdates(); else definitionUpdates = null; } private static string WriteEmptyElement(string elementName) { return "<" + elementName + " />"; } private void UpdateRecords() { if (_factoryRecords != null) { foreach (FactoryRecord value in _factoryRecords.Values) { if (string.IsNullOrEmpty(value.Filename)) value.Filename = base.ConfigStreamInfo.StreamName; value.LineNumber = 0; ConfigurationSection configSection = GetConfigSection(value.ConfigKey); if (configSection != null) { if (configSection.SectionInformation.Type != null) value.FactoryTypeName = configSection.SectionInformation.Type; value.AllowLocation = configSection.SectionInformation.AllowLocation; value.RestartOnExternalChanges = configSection.SectionInformation.RestartOnExternalChanges; value.RequirePermission = configSection.SectionInformation.RequirePermission; value.AllowDefinition = configSection.SectionInformation.AllowDefinition; value.AllowExeDefinition = configSection.SectionInformation.AllowExeDefinition; } } } if (_sectionRecords != null) { string definitionConfigPath = base.IsLocationConfig ? _parent.ConfigPath : base.ConfigPath; foreach (SectionRecord value2 in _sectionRecords.Values) { ConfigurationSection configurationSection; string text; string text2; if (value2.HasResult) { configurationSection = (ConfigurationSection)value2.Result; text = configurationSection.SectionInformation.ConfigSource; if (string.IsNullOrEmpty(text)) text = null; text2 = configurationSection.SectionInformation.ConfigSourceStreamName; if (string.IsNullOrEmpty(text2)) text2 = null; } else { configurationSection = null; text = null; text2 = null; if (value2.HasFileInput) { SectionXmlInfo sectionXmlInfo = value2.FileInput.SectionXmlInfo; text = sectionXmlInfo.ConfigSource; text2 = sectionXmlInfo.ConfigSourceStreamName; } } if (!string.IsNullOrEmpty(text)) MonitorStream(value2.ConfigKey, text, text2); if (!value2.HasResult) { if (value2.HasFileInput) value2.FileInput.SectionXmlInfo.StreamVersion = base.ConfigStreamInfo.StreamVersion; } else { configurationSection.SectionInformation.RawXml = null; bool addUpdate = value2.AddUpdate; value2.AddUpdate = false; if (addUpdate) { SectionInput sectionInput = value2.FileInput; if (sectionInput == null) { sectionInput = new SectionInput(new SectionXmlInfo(value2.ConfigKey, definitionConfigPath, _configPath, _locationSubPath, base.ConfigStreamInfo.StreamName, 0, base.ConfigStreamInfo.StreamVersion, null, text, text2, configurationSection.SectionInformation.ProtectionProviderName, configurationSection.SectionInformation.OverrideModeSetting, !configurationSection.SectionInformation.InheritInChildApplications), null) { Result = configurationSection, ResultRuntimeObject = configurationSection }; value2.AddFileInput(sectionInput); } else { SectionXmlInfo sectionXmlInfo2 = sectionInput.SectionXmlInfo; sectionXmlInfo2.LineNumber = 0; sectionXmlInfo2.StreamVersion = base.ConfigStreamInfo.StreamVersion; sectionXmlInfo2.RawXml = null; sectionXmlInfo2.ConfigSource = text; sectionXmlInfo2.ConfigSourceStreamName = text2; sectionXmlInfo2.ProtectionProviderName = configurationSection.SectionInformation.ProtectionProviderName; sectionXmlInfo2.OverrideModeSetting = configurationSection.SectionInformation.OverrideModeSetting; sectionXmlInfo2.SkipInChildApps = !configurationSection.SectionInformation.InheritInChildApplications; } sectionInput.ProtectionProvider = configurationSection.SectionInformation.ProtectionProvider; } try { configurationSection.ResetModified(); } catch (Exception inner) { throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_exception_in_config_section_handler, value2.ConfigKey), inner, base.ConfigStreamInfo.StreamName, 0); } } } } foreach (StreamInfo value3 in _streamInfoUpdates.Values) { if (!base.ConfigStreamInfo.StreamInfos.Contains(value3.StreamName)) MonitorStream(value3.SectionName, value3.ConfigSource, value3.StreamName); } InitStreamInfoUpdates(); _flags[512] = _flags[67108864]; _flags[16777216] = false; _flags[33554432] = false; if (!base.IsLocationConfig && _locationSections != null && _removedSections != null && _removedSections.Count > 0) { int num = 0; while (num < _locationSections.Count) { LocationSectionRecord locationSectionRecord = (LocationSectionRecord)_locationSections[num]; if (_removedSections.Contains(locationSectionRecord.ConfigKey)) _locationSections.RemoveAt(num); else num++; } } _removedSections = null; _removedSectionGroups = null; } private void CreateNewConfig(SectionUpdates declarationUpdates, ConfigDefinitionUpdates definitionUpdates, NamespaceChange namespaceChange, XmlUtilWriter utilWriter) { utilWriter.Write(string.Format(CultureInfo.InvariantCulture, "<?xml version=\"1.0\" encoding=\"{0}\"?>\r\n", base.ConfigStreamInfo.StreamEncoding.WebName)); if (namespaceChange == NamespaceChange.Add) utilWriter.Write(string.Format(CultureInfo.InvariantCulture, "<configuration xmlns=\"{0}\">\r\n", "http://schemas.microsoft.com/.NetConfiguration/v2.0")); else utilWriter.Write("<configuration>\r\n"); if (declarationUpdates != null) WriteNewConfigDeclarations(declarationUpdates, utilWriter, 5, 4, false); WriteNewConfigDefinitions(definitionUpdates, utilWriter, 5, 4); utilWriter.Write("</configuration>"); } private void WriteNewConfigDeclarations(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) { if (!skipFirstIndent) utilWriter.AppendSpacesToLinePosition(linePosition); utilWriter.Write("<configSections>\r\n"); WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, linePosition + indent, indent, false); utilWriter.AppendSpacesToLinePosition(linePosition); utilWriter.Write("</configSections>\r\n"); if (skipFirstIndent) utilWriter.AppendSpacesToLinePosition(linePosition); } private void WriteUnwrittenConfigDeclarations(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) { WriteUnwrittenConfigDeclarationsRecursive(declarationUpdates, utilWriter, linePosition, indent, skipFirstIndent); } private void WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) { string[] unretrievedSectionNames = declarationUpdates.GetUnretrievedSectionNames(); if (unretrievedSectionNames != null) { string[] array = unretrievedSectionNames; foreach (string configKey in array) { if (!skipFirstIndent) utilWriter.AppendSpacesToLinePosition(linePosition); skipFirstIndent = false; DeclarationUpdate declarationUpdate = declarationUpdates.GetDeclarationUpdate(configKey); if (!string.IsNullOrEmpty(declarationUpdate?.UpdatedXml)) { utilWriter.Write(declarationUpdate.UpdatedXml); utilWriter.AppendNewLine(); } } } string[] unretrievedGroupNames = declarationUpdates.GetUnretrievedGroupNames(); if (unretrievedGroupNames != null) { string[] array = unretrievedGroupNames; foreach (string text in array) { if (base.TargetFramework != (FrameworkName)null) { ConfigurationSectionGroup sectionGroup = GetSectionGroup(text); if (sectionGroup != null && !sectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework)) { declarationUpdates.MarkGroupAsRetrieved(text); continue; } } if (!skipFirstIndent) utilWriter.AppendSpacesToLinePosition(linePosition); skipFirstIndent = false; SectionUpdates sectionUpdatesForGroup = declarationUpdates.GetSectionUpdatesForGroup(text); DeclarationUpdate sectionGroupUpdate = sectionUpdatesForGroup.GetSectionGroupUpdate(); if (sectionGroupUpdate == null) utilWriter.Write("<sectionGroup name=\"" + text + "\">"); else utilWriter.Write(sectionGroupUpdate.UpdatedXml); utilWriter.AppendNewLine(); WriteUnwrittenConfigDeclarationsRecursive(sectionUpdatesForGroup, utilWriter, linePosition + indent, indent, false); utilWriter.AppendSpacesToLinePosition(linePosition); utilWriter.Write("</sectionGroup>\r\n"); } } } private void WriteNewConfigDefinitions(ConfigDefinitionUpdates configDefinitionUpdates, XmlUtilWriter utilWriter, int linePosition, int indent) { if (configDefinitionUpdates != null) { OverrideModeSetting overrideModeSetting; foreach (LocationUpdates locationUpdates in configDefinitionUpdates.LocationUpdatesList) { SectionUpdates sectionUpdates = locationUpdates.SectionUpdates; if (!sectionUpdates.IsEmpty && sectionUpdates.IsNew) { configDefinitionUpdates.FlagLocationWritten(); bool num = _locationSubPath != null || !locationUpdates.IsDefault; int num2 = linePosition; utilWriter.AppendSpacesToLinePosition(linePosition); if (num) { if (_locationSubPath == null) { CultureInfo invariantCulture = CultureInfo.InvariantCulture; overrideModeSetting = locationUpdates.OverrideMode; utilWriter.Write(string.Format(invariantCulture, "<location {0} inheritInChildApplications=\"{1}\">\r\n", overrideModeSetting.LocationTagXmlString, BoolToString(locationUpdates.InheritInChildApps))); } else { CultureInfo invariantCulture2 = CultureInfo.InvariantCulture; overrideModeSetting = locationUpdates.OverrideMode; utilWriter.Write(string.Format(invariantCulture2, "<location path=\"{2}\" {0} inheritInChildApplications=\"{1}\">\r\n", overrideModeSetting.LocationTagXmlString, BoolToString(locationUpdates.InheritInChildApps), _locationSubPath)); } num2 += indent; utilWriter.AppendSpacesToLinePosition(num2); } WriteNewConfigDefinitionsRecursive(utilWriter, locationUpdates.SectionUpdates, num2, indent, true); if (num) { utilWriter.AppendSpacesToLinePosition(linePosition); utilWriter.Write("</location>"); utilWriter.AppendNewLine(); } } } if (configDefinitionUpdates.RequireLocation) { configDefinitionUpdates.FlagLocationWritten(); utilWriter.AppendSpacesToLinePosition(linePosition); CultureInfo invariantCulture3 = CultureInfo.InvariantCulture; overrideModeSetting = OverrideModeSetting.s_locationDefault; utilWriter.Write(string.Format(invariantCulture3, "<location path=\"{2}\" {0} inheritInChildApplications=\"{1}\">\r\n", overrideModeSetting.LocationTagXmlString, "true", _locationSubPath)); utilWriter.AppendSpacesToLinePosition(linePosition); utilWriter.Write("</location>"); utilWriter.AppendNewLine(); } } } private bool WriteNewConfigDefinitionsRecursive(XmlUtilWriter utilWriter, SectionUpdates sectionUpdates, int linePosition, int indent, bool skipFirstIndent) { bool result = false; string[] movedSectionNames = sectionUpdates.GetMovedSectionNames(); if (movedSectionNames != null) { result = true; string[] array = movedSectionNames; foreach (string configKey in array) { DefinitionUpdate definitionUpdate = sectionUpdates.GetDefinitionUpdate(configKey); WriteSectionUpdate(utilWriter, definitionUpdate, linePosition, indent, skipFirstIndent); utilWriter.AppendNewLine(); skipFirstIndent = false; } } string[] newGroupNames = sectionUpdates.GetNewGroupNames(); if (newGroupNames != null) { string[] array = newGroupNames; foreach (string text in array) { if (base.TargetFramework != (FrameworkName)null) { ConfigurationSectionGroup sectionGroup = GetSectionGroup(text); if (sectionGroup != null && !sectionGroup.ShouldSerializeSectionGroupInTargetVersion(base.TargetFramework)) { sectionUpdates.MarkGroupAsRetrieved(text); continue; } } if (!skipFirstIndent) utilWriter.AppendSpacesToLinePosition(linePosition); skipFirstIndent = false; utilWriter.Write("<" + text + ">\r\n"); if (WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates.GetSectionUpdatesForGroup(text), linePosition + indent, indent, false)) result = true; utilWriter.AppendSpacesToLinePosition(linePosition); utilWriter.Write("</" + text + ">\r\n"); } } sectionUpdates.IsNew = false; return result; } private static void CheckPreamble(byte[] preamble, XmlUtilWriter utilWriter, byte[] buffer) { bool flag = false; using (MemoryStream memoryStream = new MemoryStream(buffer)) { byte[] array = new byte[preamble.Length]; if (memoryStream.Read(array, 0, array.Length) == array.Length) { flag = true; for (int i = 0; i < array.Length; i++) { if (array[i] != preamble[i]) { flag = false; break; } } } } if (!flag) { object o = utilWriter.CreateStreamCheckpoint(); utilWriter.Write('x'); utilWriter.RestoreStreamCheckpoint(o); } } private static int UpdateIndent(int oldIndent, XmlUtil xmlUtil, XmlUtilWriter utilWriter, int parentLinePosition) { int result = oldIndent; if (xmlUtil.Reader.NodeType == XmlNodeType.Element && utilWriter.IsLastLineBlank) { int trueLinePosition = xmlUtil.TrueLinePosition; if (parentLinePosition < trueLinePosition && trueLinePosition <= parentLinePosition + 10) result = trueLinePosition - parentLinePosition; } return result; } private void CopyConfig(SectionUpdates declarationUpdates, ConfigDefinitionUpdates definitionUpdates, byte[] buffer, string filename, NamespaceChange namespaceChange, XmlUtilWriter utilWriter) { CheckPreamble(base.ConfigStreamInfo.StreamEncoding.GetPreamble(), utilWriter, buffer); using (Stream stream = new MemoryStream(buffer)) using (XmlUtil xmlUtil = new XmlUtil(stream, filename, false)) { XmlTextReader reader = xmlUtil.Reader; reader.WhitespaceHandling = WhitespaceHandling.All; reader.Read(); xmlUtil.CopyReaderToNextElement(utilWriter, false); int num = 4; int trueLinePosition = xmlUtil.TrueLinePosition; bool isEmptyElement = reader.IsEmptyElement; string updatedStartElement; object obj; switch (namespaceChange) { case NamespaceChange.Add: updatedStartElement = string.Format(CultureInfo.InvariantCulture, "<configuration xmlns=\"{0}\">\r\n", "http://schemas.microsoft.com/.NetConfiguration/v2.0"); break; default: obj = null; goto IL_0082; case NamespaceChange.Remove: { obj = "<configuration>\r\n"; goto IL_0082; } IL_0082: updatedStartElement = (string)obj; break; } bool needsChildren = declarationUpdates != null || definitionUpdates != null; string text = xmlUtil.UpdateStartElement(utilWriter, updatedStartElement, needsChildren, trueLinePosition, num); bool flag = false; if (!isEmptyElement) { xmlUtil.CopyReaderToNextElement(utilWriter, true); num = UpdateIndent(num, xmlUtil, utilWriter, trueLinePosition); if (reader.NodeType == XmlNodeType.Element && reader.Name == "configSections") { flag = true; int trueLinePosition2 = xmlUtil.TrueLinePosition; bool isEmptyElement2 = reader.IsEmptyElement; if (declarationUpdates == null) xmlUtil.CopyOuterXmlToNextElement(utilWriter, true); else { string text2 = xmlUtil.UpdateStartElement(utilWriter, null, true, trueLinePosition2, num); if (!isEmptyElement2) { xmlUtil.CopyReaderToNextElement(utilWriter, true); CopyConfigDeclarationsRecursive(declarationUpdates, xmlUtil, utilWriter, string.Empty, trueLinePosition2, num); } if (declarationUpdates.HasUnretrievedSections()) { int linePosition = 0; if (text2 == null) linePosition = xmlUtil.TrueLinePosition; if (!utilWriter.IsLastLineBlank) utilWriter.AppendNewLine(); WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, trueLinePosition2 + num, num, false); if (text2 == null) utilWriter.AppendSpacesToLinePosition(linePosition); } if (text2 == null) xmlUtil.CopyXmlNode(utilWriter); else utilWriter.Write(text2); xmlUtil.CopyReaderToNextElement(utilWriter, true); } } } if (!flag && declarationUpdates != null) { bool flag2 = reader.Depth > 0 && reader.NodeType == XmlNodeType.Element; int linePosition2 = (!flag2) ? (trueLinePosition + num) : xmlUtil.TrueLinePosition; WriteNewConfigDeclarations(declarationUpdates, utilWriter, linePosition2, num, flag2); } if (definitionUpdates != null) { bool locationPathApplies = false; LocationUpdates locationUpdates = null; SectionUpdates sectionUpdates = null; if (!base.IsLocationConfig) { locationPathApplies = true; locationUpdates = definitionUpdates.FindLocationUpdates(OverrideModeSetting.s_locationDefault, true); if (locationUpdates != null) sectionUpdates = locationUpdates.SectionUpdates; } CopyConfigDefinitionsRecursive(definitionUpdates, xmlUtil, utilWriter, locationPathApplies, locationUpdates, sectionUpdates, true, string.Empty, trueLinePosition, num); WriteNewConfigDefinitions(definitionUpdates, utilWriter, trueLinePosition + num, num); } if (text != null) { if (!utilWriter.IsLastLineBlank) utilWriter.AppendNewLine(); utilWriter.Write(text); } while (xmlUtil.CopyXmlNode(utilWriter)) { } } } private static bool CopyConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, string group, int parentLinePosition, int parentIndent) { bool result = false; XmlTextReader reader = xmlUtil.Reader; int num = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition); int linePosition; int num2; switch (reader.NodeType) { case XmlNodeType.Element: num2 = xmlUtil.TrueLinePosition; linePosition = num2; break; case XmlNodeType.EndElement: num2 = parentLinePosition + num; linePosition = (utilWriter.IsLastLineBlank ? xmlUtil.TrueLinePosition : parentLinePosition); break; default: num2 = parentLinePosition + num; linePosition = 0; break; } string[] array = declarationUpdates?.GetMovedSectionNames(); if (array != null) { if (!utilWriter.IsLastLineBlank) utilWriter.AppendNewLine(); string[] array2 = array; foreach (string configKey in array2) { DeclarationUpdate declarationUpdate = declarationUpdates.GetDeclarationUpdate(configKey); utilWriter.AppendSpacesToLinePosition(num2); utilWriter.Write(declarationUpdate.UpdatedXml); utilWriter.AppendNewLine(); result = true; } utilWriter.AppendSpacesToLinePosition(linePosition); } if (reader.NodeType == XmlNodeType.Element) { int depth = reader.Depth; while (reader.Depth == depth) { bool flag = false; DeclarationUpdate declarationUpdate2 = null; DeclarationUpdate declarationUpdate3 = null; SectionUpdates sectionUpdates = null; SectionUpdates declarationUpdates2 = declarationUpdates; string group2 = group; num = UpdateIndent(num, xmlUtil, utilWriter, parentLinePosition); num2 = xmlUtil.TrueLinePosition; string name = reader.Name; string attribute = reader.GetAttribute("name"); string text = BaseConfigurationRecord.CombineConfigKey(group, attribute); if (name == "sectionGroup") { sectionUpdates = declarationUpdates.GetSectionUpdatesForGroup(attribute); if (sectionUpdates != null) { declarationUpdate3 = sectionUpdates.GetSectionGroupUpdate(); if (sectionUpdates.HasUnretrievedSections()) { flag = true; group2 = text; declarationUpdates2 = sectionUpdates; } } } else declarationUpdate2 = declarationUpdates.GetDeclarationUpdate(text); bool flag2 = declarationUpdate3?.UpdatedXml != null; if (flag) { object o = utilWriter.CreateStreamCheckpoint(); string text2 = null; if (flag2) { utilWriter.Write(declarationUpdate3.UpdatedXml); reader.Read(); } else text2 = xmlUtil.UpdateStartElement(utilWriter, null, true, num2, num); if (text2 == null) xmlUtil.CopyReaderToNextElement(utilWriter, true); bool num3 = CopyConfigDeclarationsRecursive(declarationUpdates2, xmlUtil, utilWriter, group2, num2, num); if (text2 != null) { utilWriter.AppendSpacesToLinePosition(num2); utilWriter.Write(text2); utilWriter.AppendSpacesToLinePosition(parentLinePosition); } else xmlUtil.CopyXmlNode(utilWriter); if (num3 | flag2) result = true; else utilWriter.RestoreStreamCheckpoint(o); xmlUtil.CopyReaderToNextElement(utilWriter, true); } else { bool flag3 = false; bool flag4; if (declarationUpdate2 == null) { flag4 = true; if (flag2) { result = true; utilWriter.Write(declarationUpdate3.UpdatedXml); utilWriter.AppendNewLine(); utilWriter.AppendSpacesToLinePosition(num2); utilWriter.Write("</sectionGroup>"); utilWriter.AppendNewLine(); utilWriter.AppendSpacesToLinePosition(num2); } else if (declarationUpdate3 != null) { result = true; flag4 = false; flag3 = true; } } else { result = true; if (declarationUpdate2.UpdatedXml == null) flag4 = false; else { flag4 = true; utilWriter.Write(declarationUpdate2.UpdatedXml); } } if (flag4) xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true); else if (flag3) { xmlUtil.SkipChildElementsAndCopyOuterXmlToNextElement(utilWriter); } else { xmlUtil.CopyOuterXmlToNextElement(utilWriter, true); } } } } return result; } private bool CopyConfigDefinitionsRecursive(ConfigDefinitionUpdates configDefinitionUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, bool locationPathApplies, LocationUpdates locationUpdates, SectionUpdates sectionUpdates, bool addNewSections, string group, int parentLinePosition, int parentIndent) { bool result = false; XmlTextReader reader = xmlUtil.Reader; int num = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition); int num2; int linePosition; if (reader.NodeType == XmlNodeType.Element) { num2 = xmlUtil.TrueLinePosition; linePosition = num2; } else if (reader.NodeType == XmlNodeType.EndElement) { num2 = parentLinePosition + num; linePosition = (utilWriter.IsLastLineBlank ? xmlUtil.TrueLinePosition : parentLinePosition); } else { num2 = parentLinePosition + num; linePosition = 0; } if ((sectionUpdates != null) & addNewSections) { sectionUpdates.IsNew = false; string[] movedSectionNames = sectionUpdates.GetMovedSectionNames(); if (movedSectionNames != null) { if (!utilWriter.IsLastLineBlank) utilWriter.AppendNewLine(); utilWriter.AppendSpacesToLinePosition(num2); bool skipFirstIndent = true; string[] array = movedSectionNames; foreach (string configKey in array) { DefinitionUpdate definitionUpdate = sectionUpdates.GetDefinitionUpdate(configKey); WriteSectionUpdate(utilWriter, definitionUpdate, num2, num, skipFirstIndent); skipFirstIndent = false; utilWriter.AppendNewLine(); result = true; } utilWriter.AppendSpacesToLinePosition(linePosition); } } if (reader.NodeType == XmlNodeType.Element) { int depth = reader.Depth; while (reader.Depth == depth) { bool flag = false; DefinitionUpdate definitionUpdate2 = null; bool flag2 = locationPathApplies; LocationUpdates locationUpdates2 = locationUpdates; SectionUpdates sectionUpdates2 = sectionUpdates; bool addNewSections2 = addNewSections; string group2 = group; bool flag3 = false; num = UpdateIndent(num, xmlUtil, utilWriter, parentLinePosition); num2 = xmlUtil.TrueLinePosition; string name = reader.Name; if (name == "location") { string attribute = reader.GetAttribute("path"); attribute = BaseConfigurationRecord.NormalizeLocationSubPath(attribute, xmlUtil); OverrideModeSetting overrideMode = OverrideModeSetting.s_locationDefault; bool inheritInChildApps = true; flag2 = ((!base.IsLocationConfig) ? (attribute == null) : (attribute != null && StringUtil.EqualsIgnoreCase(base.ConfigPath, base.Host.GetConfigPathFromLocationSubPath(base.Parent.ConfigPath, attribute)))); if (flag2) { string attribute2 = reader.GetAttribute("allowOverride"); if (attribute2 != null) overrideMode = OverrideModeSetting.CreateFromXmlReadValue(bool.Parse(attribute2)); string attribute3 = reader.GetAttribute("overrideMode"); if (attribute3 != null) overrideMode = OverrideModeSetting.CreateFromXmlReadValue(OverrideModeSetting.ParseOverrideModeXmlValue(attribute3, null)); string attribute4 = reader.GetAttribute("inheritInChildApplications"); if (attribute4 != null) inheritInChildApps = bool.Parse(attribute4); configDefinitionUpdates.FlagLocationWritten(); } if (reader.IsEmptyElement) flag2 = ((flag2 && configDefinitionUpdates.FindLocationUpdates(overrideMode, inheritInChildApps) != null) ? true : false); else if (flag2) { if (configDefinitionUpdates != null) { locationUpdates2 = configDefinitionUpdates.FindLocationUpdates(overrideMode, inheritInChildApps); if (locationUpdates2 != null) { flag = true; sectionUpdates2 = locationUpdates2.SectionUpdates; if (_locationSubPath == null && locationUpdates2.IsDefault) addNewSections2 = false; } } } else if (HasRemovedSectionsOrGroups && !base.IsLocationConfig && base.Host.SupportsLocation) { flag = true; locationUpdates2 = null; sectionUpdates2 = null; addNewSections2 = false; } } else { string text = BaseConfigurationRecord.CombineConfigKey(group, name); FactoryRecord factoryRecord = FindFactoryRecord(text, false); if (factoryRecord == null) { if (!flag2 && !base.IsLocationConfig) flag3 = true; } else if (factoryRecord.IsGroup) { if (reader.IsEmptyElement) { if (!flag2 && !base.IsLocationConfig) flag3 = true; } else if (sectionUpdates != null) { SectionUpdates sectionUpdatesForGroup = sectionUpdates.GetSectionUpdatesForGroup(name); if (sectionUpdatesForGroup != null) { flag = true; group2 = text; sectionUpdates2 = sectionUpdatesForGroup; } } else if (!flag2 && !base.IsLocationConfig) { if (_removedSectionGroups != null && _removedSectionGroups.Contains(text)) flag3 = true; else { flag = true; group2 = text; locationUpdates2 = null; sectionUpdates2 = null; addNewSections2 = false; } } } else if (sectionUpdates != null) { definitionUpdate2 = sectionUpdates.GetDefinitionUpdate(text); } else if (!flag2 && !base.IsLocationConfig && _removedSections != null && _removedSections.Contains(text)) { flag3 = true; } } if (flag) { object o = utilWriter.CreateStreamCheckpoint(); xmlUtil.CopyXmlNode(utilWriter); xmlUtil.CopyReaderToNextElement(utilWriter, true); bool num3 = CopyConfigDefinitionsRecursive(configDefinitionUpdates, xmlUtil, utilWriter, flag2, locationUpdates2, sectionUpdates2, addNewSections2, group2, num2, num); xmlUtil.CopyXmlNode(utilWriter); if (num3) result = true; else utilWriter.RestoreStreamCheckpoint(o); xmlUtil.CopyReaderToNextElement(utilWriter, true); } else { bool flag4; if (definitionUpdate2 == null) flag4 = (flag2 | flag3); else { flag4 = false; if (definitionUpdate2.UpdatedXml != null) { ConfigurationSection configurationSection = (ConfigurationSection)definitionUpdate2.SectionRecord.Result; if (string.IsNullOrEmpty(configurationSection.SectionInformation.ConfigSource) || configurationSection.SectionInformation.ConfigSourceModified) { flag4 = true; WriteSectionUpdate(utilWriter, definitionUpdate2, num2, num, true); result = true; } } } if (flag4) xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true); else { xmlUtil.CopyOuterXmlToNextElement(utilWriter, true); result = true; } } } } if (((sectionUpdates != null) & addNewSections) && sectionUpdates.HasNewSectionGroups()) { num2 = parentLinePosition + num; linePosition = ((reader.NodeType == XmlNodeType.EndElement) ? (utilWriter.IsLastLineBlank ? xmlUtil.TrueLinePosition : parentLinePosition) : 0); utilWriter.AppendSpacesToLinePosition(num2); if (WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates, num2, num, true)) result = true; utilWriter.AppendSpacesToLinePosition(linePosition); } return result; } private static void WriteSectionUpdate(XmlUtilWriter utilWriter, DefinitionUpdate update, int linePosition, int indent, bool skipFirstIndent) { ConfigurationSection configurationSection = (ConfigurationSection)update.SectionRecord.Result; string xmlElement = string.IsNullOrEmpty(configurationSection.SectionInformation.ConfigSource) ? update.UpdatedXml : string.Format(CultureInfo.InvariantCulture, "<{0} configSource=\"{1}\" />", configurationSection.SectionInformation.Name, configurationSection.SectionInformation.ConfigSource); string s = XmlUtil.FormatXmlElement(xmlElement, linePosition, indent, skipFirstIndent); utilWriter.Write(s); } private void SaveConfigSource(DefinitionUpdate update) { string text = (!update.SectionRecord.HasResult) ? update.SectionRecord.FileInput.SectionXmlInfo.ConfigSourceStreamName : ((ConfigurationSection)update.SectionRecord.Result).SectionInformation.ConfigSourceStreamName; byte[] array = null; using (Stream stream = base.Host.OpenStreamForRead(text)) { if (stream != null) { array = new byte[stream.Length]; if (stream.Read(array, 0, (int)stream.Length) != stream.Length) throw new ConfigurationErrorsException(); } } bool flag = array != null; object writeContext = null; bool flag2 = false; try { try { string templateStreamName = base.Host.IsRemote ? null : base.ConfigStreamInfo.StreamName; using (Stream stream2 = base.Host.OpenStreamForWrite(text, templateStreamName, ref writeContext)) { flag2 = true; if (update.UpdatedXml == null) { if (flag) stream2.Write(array, 0, array.Length); } else { using (StreamWriter writer = new StreamWriter(stream2)) { XmlUtilWriter utilWriter = new XmlUtilWriter(writer, true); if (flag) CopyConfigSource(utilWriter, update.UpdatedXml, text, array); else CreateNewConfigSource(utilWriter, update.UpdatedXml, 4); } } } } catch { if (flag2) base.Host.WriteCompleted(text, false, writeContext); throw; } } catch (Exception e) { throw ExceptionUtil.WrapAsConfigException(System.SR.Config_error_loading_XML_file, e, text, 0); } base.Host.WriteCompleted(text, true, writeContext); } private void CopyConfigSource(XmlUtilWriter utilWriter, string updatedXml, string configSourceStreamName, byte[] buffer) { byte[] preamble = default(byte[]); using (Stream stream = new MemoryStream(buffer)) using (new XmlUtil(stream, configSourceStreamName, true)) preamble = base.ConfigStreamInfo.StreamEncoding.GetPreamble(); CheckPreamble(preamble, utilWriter, buffer); using (Stream stream2 = new MemoryStream(buffer)) using (XmlUtil xmlUtil2 = new XmlUtil(stream2, configSourceStreamName, false)) { XmlTextReader reader = xmlUtil2.Reader; reader.WhitespaceHandling = WhitespaceHandling.All; reader.Read(); int indent = 4; int num = 1; bool flag = xmlUtil2.CopyReaderToNextElement(utilWriter, false); if (flag) { int lineNumber = reader.LineNumber; num = reader.LinePosition - 1; int num2 = 0; while (reader.MoveToNextAttribute()) { if (reader.LineNumber > lineNumber) { num2 = reader.LinePosition - num; break; } } int num3 = 0; reader.Read(); while (reader.Depth >= 1) { if (reader.NodeType == XmlNodeType.Element) { num3 = reader.LinePosition - 1 - num; break; } reader.Read(); } if (num3 > 0) indent = num3; else if (num2 > 0) { indent = num2; } } string s = XmlUtil.FormatXmlElement(updatedXml, num, indent, true); utilWriter.Write(s); if (flag) { while (reader.Depth > 0) { reader.Read(); } if (reader.IsEmptyElement || reader.NodeType == XmlNodeType.EndElement) reader.Read(); while (xmlUtil2.CopyXmlNode(utilWriter)) { } } } } private void CreateNewConfigSource(XmlUtilWriter utilWriter, string updatedXml, int indent) { string str = XmlUtil.FormatXmlElement(updatedXml, 0, indent, true); utilWriter.Write(string.Format(CultureInfo.InvariantCulture, "<?xml version=\"1.0\" encoding=\"{0}\"?>\r\n", base.ConfigStreamInfo.StreamEncoding.WebName)); utilWriter.Write(str + "\r\n"); } private static string BoolToString(bool v) { if (!v) return "false"; return "true"; } internal void RemoveLocationWriteRequirement() { if (base.IsLocationConfig) { _flags[16777216] = false; _flags[33554432] = true; } } } }