NameValueFileSectionHandler
Provides access to a configuration file. This type supports the .NET configuration infrastructure and is not intended to be used directly from your code.
using System.Configuration.Internal;
using System.IO;
using System.Xml;
namespace System.Configuration
{
public class NameValueFileSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
XmlNode xmlNode = section.Attributes.RemoveNamedItem("file");
object obj = NameValueSectionHandler.CreateStatic(parent, section);
if (xmlNode != null && xmlNode.Value.Length != 0) {
string value = xmlNode.Value;
IConfigErrorInfo configErrorInfo = xmlNode as IConfigErrorInfo;
if (configErrorInfo == null)
return null;
string text = Path.Combine(Path.GetDirectoryName(configErrorInfo.Filename), value);
if (File.Exists(text)) {
ConfigXmlDocument configXmlDocument = new ConfigXmlDocument();
try {
configXmlDocument.Load(text);
} catch (XmlException ex) {
throw new ConfigurationErrorsException(ex.Message, ex, text, ex.LineNumber);
}
if (section.Name != configXmlDocument.DocumentElement.Name)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_name_value_file_section_file_invalid_root, section.Name), configXmlDocument.DocumentElement);
obj = NameValueSectionHandler.CreateStatic(obj, configXmlDocument.DocumentElement);
}
}
return obj;
}
}
}