【发布时间】:2017-02-04 03:33:42
【问题描述】:
我正在尝试使用标准 XmlSerializer 从当前应用程序/webapp 的配置文件中加载自定义部分。
以下解决方案不适用于网络应用:
var configFilePath =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.FilePath;
if (configFilePath == null || !File.Exists(configFilePath))
return null;
var xdoc = XDocument.Load(configFilePath);
var section =
xdoc.Root?.Element("ParentSection")?.Element("MySection");
if (section == null)
return null;
var serializer = new XmlSerializer(typeof(MyConfigType));
MyConfigType config;
using (var reader = new StringReader(section.ToString()))
{
config = serializer.Deserialize(reader) as MyConfigType;
}
如何在不检查当前应用是否为网络应用的情况下轻松完成此操作?
(不使用System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration?)
【问题讨论】:
标签: c# .net web-config app-config configuration-files