如何使路径可配置?

Mac*_*que 1 c# asp.net linq-to-xml

我使用以下语法

XDocument config = XDocument.Load(@"path to the xml file");
Run Code Online (Sandbox Code Playgroud)

但是我在 c# 代码中包含此语句。我想让路径可配置,例如在应用程序的 web.config 文件中使用名称路径声明一个键,我应该能够通过 xdocument cofnig 在 c# 代码中获得它=xdocument.Load(路径)。

可以这样吗?

Bre*_*ias 5

也许做通常的事情就足够了:

const string key="xmlPath";
...    
string path = ConfigurationManager.AppSettings[key];
XDocument config = XDocument.Load(path);     
Run Code Online (Sandbox Code Playgroud)

这假设 web.config 包含:

<appSettings>
  <add key="xmlPath" value="c:\path\to\xml\file.xml" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)