ConfigurationSection
Represents a section within a configuration file.
using System.Globalization;
using System.IO;
using System.Runtime.Versioning;
using System.Xml;
namespace System.Configuration
{
public abstract class ConfigurationSection : ConfigurationElement
{
public SectionInformation SectionInformation { get; }
protected ConfigurationSection()
{
SectionInformation = new SectionInformation(this);
}
protected internal virtual object GetRuntimeObject()
{
return this;
}
protected internal override bool IsModified()
{
if (!SectionInformation.IsModifiedFlags())
return base.IsModified();
return true;
}
protected internal override void ResetModified()
{
SectionInformation.ResetModifiedFlags();
base.ResetModified();
}
protected internal virtual void DeserializeSection(XmlReader reader)
{
if (!reader.Read() || reader.NodeType != XmlNodeType.Element)
throw new ConfigurationErrorsException(System.SR.Config_base_expected_to_find_element, reader);
DeserializeElement(reader, false);
}
protected internal virtual string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
{
if (base.CurrentConfiguration != null && base.CurrentConfiguration.TargetFramework != (FrameworkName)null && !ShouldSerializeSectionInTargetVersion(base.CurrentConfiguration.TargetFramework))
return string.Empty;
ConfigurationElement.ValidateElement(this, null, true);
ConfigurationElement configurationElement = CreateElement(GetType());
configurationElement.Unmerge(this, parentElement, saveMode);
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter) {
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
configurationElement.DataToWriteInternal = (saveMode != ConfigurationSaveMode.Minimal);
if (base.CurrentConfiguration != null && base.CurrentConfiguration.TargetFramework != (FrameworkName)null)
_configRecord.SectionsStack.Push(this);
configurationElement.SerializeToXmlElement(xmlTextWriter, name);
if (base.CurrentConfiguration != null && base.CurrentConfiguration.TargetFramework != (FrameworkName)null)
_configRecord.SectionsStack.Pop();
xmlTextWriter.Flush();
return stringWriter.ToString();
}
protected internal virtual bool ShouldSerializePropertyInTargetVersion(ConfigurationProperty property, string propertyName, FrameworkName targetFramework, ConfigurationElement parentConfigurationElement)
{
return true;
}
protected internal virtual bool ShouldSerializeElementInTargetVersion(ConfigurationElement element, string elementName, FrameworkName targetFramework)
{
return true;
}
protected internal virtual bool ShouldSerializeSectionInTargetVersion(FrameworkName targetFramework)
{
return true;
}
}
}