// Gather all the updates to the configuration section declarations.
        private SectionUpdates GetConfigDeclarationUpdates(ConfigurationSaveMode saveMode, bool forceUpdateAll) {
            if (IsLocationConfig)
                return null;

            // hasChanged will be set to true if there is any change that will impact the current config file.
            bool hasChanged = HasRemovedSectionsOrGroups;
            SectionUpdates sectionUpdates = new SectionUpdates(string.Empty);

            if (_factoryRecords != null) {
                foreach (FactoryRecord factoryRecord in _factoryRecords.Values) {
                    if (!factoryRecord.IsGroup) {
                        string updatedXml = null;

                        // Never write out an undeclared section.
                        if (factoryRecord.IsUndeclared) {
                            continue;
                        }

                        // Note that GetConfigSection will return only those sections that have a sectionRecord
                        // and has a result.  In another word, only sections that have been accessed.
                        ConfigurationSection configSection = GetConfigSection(factoryRecord.ConfigKey);

                        if (configSection != null) {

                            // We should skip this section declaration only if all below hold true:
                            // 1. The section should not be declared at this level.  Reasons:
                            //      i. The section is originally not declared at this level, or
                            //      ii. The user calls SectionInformation.ForceDeclaration(false)
                            // 2. It's not machine.config.  Otherwise we must declare it even if the user called ForceDeclaration(false)
                            // 3. It's already declared higher up.
                            // 4. It's not valid in the current Target Framework version
                            if (!configSection.SectionInformation.IsDeclared
                                    && !MgmtParent.IsRootConfig
                                    && MgmtParent.FindFactoryRecord(factoryRecord.ConfigKey, false) != null) {

                                if (factoryRecord.HasFile) {
                                    hasChanged = true;
                                }

                                continue;
                            }
                            if (TargetFramework != null &&
                                !configSection.ShouldSerializeSectionInTargetVersion(TargetFramework))
                            {
                                continue;
                            }

                            if (AreDeclarationAttributesModified(factoryRecord, configSection) || !factoryRecord.HasFile) {
                                updatedXml = GetUpdatedSectionDeclarationXml(factoryRecord, configSection, saveMode);
                                if (!string.IsNullOrEmpty(updatedXml))
                                    hasChanged = true;
                            }
                        }

                        DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, !factoryRecord.HasFile, updatedXml);
                        sectionUpdates.AddSection(update);
                    }
                    else {
                        bool addGroupUpdate = false;

                        // LookupSectionGroup will return an object only if the group has been accessed
                        ConfigurationSectionGroup configSectionGroup = LookupSectionGroup(factoryRecord.ConfigKey);

                        if (!factoryRecord.HasFile) {
                            // Not in the file, so it means the group is added programmatically.
                            addGroupUpdate = true;
                        }
                        else if (configSectionGroup != null && configSectionGroup.IsDeclarationRequired) {
                            // The section group is declared in this config file
                            addGroupUpdate = true;
                        }
                        else if (factoryRecord.FactoryTypeName != null || configSectionGroup != null) {
                            FactoryRecord parentFactoryRecord = null;
                            if (!MgmtParent.IsRootConfig) {
                                parentFactoryRecord = MgmtParent.FindFactoryRecord(factoryRecord.ConfigKey, false);
                            }

                            // Add it if declaration is required.  Please note this check is identical to the check
                            // for _declarationRequired in ConfigurationSectionGroup.AttachToConfigurationRecord.
                            addGroupUpdate = (parentFactoryRecord == null || parentFactoryRecord.FactoryTypeName == null);
                        }

                        if (addGroupUpdate) {
                            string updatedXml = null;

                            if (!factoryRecord.HasFile
                                    || (configSectionGroup != null && configSectionGroup.Type != factoryRecord.FactoryTypeName)) {

                                updatedXml = GetUpdatedSectionGroupDeclarationXml(factoryRecord, configSectionGroup);
                                if (!string.IsNullOrEmpty(updatedXml)) {
                                    hasChanged = true;
                                }
                            }

                            Debug.Assert(!factoryRecord.IsUndeclared, "!factoryRecord.IsUndeclared");
                            Debug.Assert(!IsImplicitSection(factoryRecord.ConfigKey), "We should never write out an implicit section");

                            DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, !factoryRecord.HasFile, updatedXml);
                            sectionUpdates.AddSectionGroup(update);
                        }
                    }
                }
            }

            if (_sectionRecords != null) {
                foreach (SectionRecord sectionRecord in _sectionRecords.Values) {
                    if (GetFactoryRecord(sectionRecord.ConfigKey, false) != null || !sectionRecord.HasResult) {
                        // Skip because this factory is defined locally ( in
                        // which case we handled above), or it was not used
                        continue;
                    }

                    ConfigurationSection configSection = (ConfigurationSection) sectionRecord.Result;
                    FactoryRecord factoryRecord = MgmtParent.FindFactoryRecord(sectionRecord.ConfigKey, false);

                    // Add this section declaration if:
                    // 1. The section is not declared locally (otherwise it's handled above)
                    // 2. SectionInformation.IsDeclared is true (i.e. user called SectionInformation.ForceDeclaration(true))
                    if (configSection.SectionInformation.IsDeclared) {
                        Debug.Assert(!IsImplicitSection(sectionRecord.ConfigKey), "We should never write out an implicit section");
                        Debug.Assert(!factoryRecord.IsUndeclared, "!factoryRecord.IsUndeclared");
                        string updatedXml = GetUpdatedSectionDeclarationXml(factoryRecord, configSection, saveMode);
                        if (!string.IsNullOrEmpty(updatedXml)) {
                            hasChanged = true;
                            DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, true, updatedXml);
                            sectionUpdates.AddSection(update);
                        }
                    }
                }
            }

            if (_sectionGroups != null) {
                foreach (ConfigurationSectionGroup configSectionGroup in _sectionGroups.Values) {
                    if (GetFactoryRecord(configSectionGroup.SectionGroupName, false) != null) {
                        continue;
                    }

                    FactoryRecord factoryRecord = MgmtParent.FindFactoryRecord(configSectionGroup.SectionGroupName, false);
                    if (    configSectionGroup.IsDeclared ||
                            (factoryRecord != null && configSectionGroup.Type != factoryRecord.FactoryTypeName)) {

                        string updatedXml = GetUpdatedSectionGroupDeclarationXml(factoryRecord, configSectionGroup);
                        if (!string.IsNullOrEmpty(updatedXml)) {
                            hasChanged = true;
                            DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, true, updatedXml);
                            sectionUpdates.AddSectionGroup(update);
                        }
                    }
                }
            }

            if (hasChanged) {
                return sectionUpdates;
            }
            else {
                return null;
            }
        }
 private SectionUpdates GetConfigDeclarationUpdates(ConfigurationSaveMode saveMode, bool forceUpdateAll)
 {
     if (!base.IsLocationConfig)
     {
         bool hasRemovedSectionsOrGroups = this.HasRemovedSectionsOrGroups;
         SectionUpdates updates = new SectionUpdates(string.Empty);
         if (base._factoryRecords != null)
         {
             foreach (FactoryRecord record in base._factoryRecords.Values)
             {
                 if (!record.IsGroup)
                 {
                     string str = null;
                     if (!record.IsUndeclared)
                     {
                         ConfigurationSection configSection = this.GetConfigSection(record.ConfigKey);
                         if (configSection != null)
                         {
                             if ((!configSection.SectionInformation.IsDeclared && !this.MgmtParent.IsRootConfig) && (this.MgmtParent.FindFactoryRecord(record.ConfigKey, false) != null))
                             {
                                 if (record.HasFile)
                                 {
                                     hasRemovedSectionsOrGroups = true;
                                 }
                                 continue;
                             }
                             if ((base.TargetFramework != null) && !configSection.ShouldSerializeSectionInTargetVersion(base.TargetFramework))
                             {
                                 continue;
                             }
                             if (this.AreDeclarationAttributesModified(record, configSection) || !record.HasFile)
                             {
                                 str = this.GetUpdatedSectionDeclarationXml(record, configSection, saveMode);
                                 if (!string.IsNullOrEmpty(str))
                                 {
                                     hasRemovedSectionsOrGroups = true;
                                 }
                             }
                         }
                         DeclarationUpdate update = new DeclarationUpdate(record.ConfigKey, !record.HasFile, str);
                         updates.AddSection(update);
                     }
                 }
                 else
                 {
                     bool flag2 = false;
                     ConfigurationSectionGroup sectionGroup = this.LookupSectionGroup(record.ConfigKey);
                     if (!record.HasFile)
                     {
                         flag2 = true;
                     }
                     else if ((sectionGroup != null) && sectionGroup.IsDeclarationRequired)
                     {
                         flag2 = true;
                     }
                     else if ((record.FactoryTypeName != null) || (sectionGroup != null))
                     {
                         FactoryRecord record2 = null;
                         if (!this.MgmtParent.IsRootConfig)
                         {
                             record2 = this.MgmtParent.FindFactoryRecord(record.ConfigKey, false);
                         }
                         flag2 = (record2 == null) || (record2.FactoryTypeName == null);
                     }
                     if (flag2)
                     {
                         string updatedSectionGroupDeclarationXml = null;
                         if (!record.HasFile || ((sectionGroup != null) && (sectionGroup.Type != record.FactoryTypeName)))
                         {
                             updatedSectionGroupDeclarationXml = this.GetUpdatedSectionGroupDeclarationXml(record, sectionGroup);
                             if (!string.IsNullOrEmpty(updatedSectionGroupDeclarationXml))
                             {
                                 hasRemovedSectionsOrGroups = true;
                             }
                         }
                         DeclarationUpdate update2 = new DeclarationUpdate(record.ConfigKey, !record.HasFile, updatedSectionGroupDeclarationXml);
                         updates.AddSectionGroup(update2);
                     }
                 }
             }
         }
         if (base._sectionRecords != null)
         {
             foreach (SectionRecord record3 in base._sectionRecords.Values)
             {
                 if ((base.GetFactoryRecord(record3.ConfigKey, false) == null) && record3.HasResult)
                 {
                     ConfigurationSection result = (ConfigurationSection) record3.Result;
                     FactoryRecord factoryRecord = this.MgmtParent.FindFactoryRecord(record3.ConfigKey, false);
                     if (result.SectionInformation.IsDeclared)
                     {
                         string str3 = this.GetUpdatedSectionDeclarationXml(factoryRecord, result, saveMode);
                         if (!string.IsNullOrEmpty(str3))
                         {
                             hasRemovedSectionsOrGroups = true;
                             DeclarationUpdate update3 = new DeclarationUpdate(factoryRecord.ConfigKey, true, str3);
                             updates.AddSection(update3);
                         }
                     }
                 }
             }
         }
         if (this._sectionGroups != null)
         {
             foreach (ConfigurationSectionGroup group2 in this._sectionGroups.Values)
             {
                 if (base.GetFactoryRecord(group2.SectionGroupName, false) == null)
                 {
                     FactoryRecord record5 = this.MgmtParent.FindFactoryRecord(group2.SectionGroupName, false);
                     if (group2.IsDeclared || ((record5 != null) && (group2.Type != record5.FactoryTypeName)))
                     {
                         string str4 = this.GetUpdatedSectionGroupDeclarationXml(record5, group2);
                         if (!string.IsNullOrEmpty(str4))
                         {
                             hasRemovedSectionsOrGroups = true;
                             DeclarationUpdate update4 = new DeclarationUpdate(record5.ConfigKey, true, str4);
                             updates.AddSectionGroup(update4);
                         }
                     }
                 }
             }
         }
         if (hasRemovedSectionsOrGroups)
         {
             return updates;
         }
     }
     return null;
 }