Microsoft.Web.Administration.ConfigurationElement.GetElementAtParentLocationInFileContext C# (CSharp) Method

GetElementAtParentLocationInFileContext() private method

private GetElementAtParentLocationInFileContext ( FileContext core ) : ConfigurationElement
core FileContext
return ConfigurationElement
        internal ConfigurationElement GetElementAtParentLocationInFileContext(FileContext core)
        {
            if (Section == null)
            {
                return null;
            }

            if (Section.Location == null)
            {
                if (core != null && core.Parent == null && Schema.Path == "configProtectedData/providers")
                {
                    // IMPORTANT: to load providers from machine.config.
                    return core.GetSection(Section.SectionPath).GetChildElement("providers");
                }

                return null;
            }

            string parentLocation = Section.Location.GetParentPath();
            while (true)
            {
                var parentSection = core.GetSection(Section.SectionPath, parentLocation);
                // IMPORTANT: allow null section as web.config does not have the sections.
                var parentElement = parentSection?.GetElementByPath(Schema.Path);
                if (parentElement != null)
                {
                    return parentElement;
                }

                if (parentLocation == null)
                {
                    return null;
                }

                parentLocation = parentLocation.GetParentPath();
            }
        }

Usage Example

        internal object ExtractDefaultValueFromSchema()
        {
            object defaultValue;

            if (_element.FileContext.Parent != null)
            {
                // web.config
                var parentElementInFile = _element.GetParentElement();
                defaultValue = parentElementInFile == null ? Schema?.DefaultValue : parentElementInFile.Attributes[Name].Value;
            }
            else if (_element.Section?.Location == null)
            {
                // root config
                defaultValue = Decrypt(Schema?.DefaultValue);
            }
            else
            {
                // location tags in applicationHost.config.
                var parentElement = _element.GetElementAtParentLocationInFileContext(_element.FileContext);
                defaultValue = parentElement == null ? Schema?.DefaultValue : parentElement.Attributes[Name].Value;
            }

            if (defaultValue == null)
            {
                // IMPORTANT: to set default values so that in configuration files they do not appear.
                if (Schema != null)
                {
                    if (Schema.DefaultValue == null)
                    {
                        try
                        {
                            Schema.SetDefaultValue(Schema.DefaultValueByType);
                        }
                        catch (COMException)
                        {
                            // IMPORTANT: If validators do not like default value, simply ignore.
                            return(defaultValue = Schema.DefaultValueByType);
                        }

                        defaultValue = Schema.DefaultValue;
                    }
                }
            }

            return(defaultValue);
        }