Macnique
Macnique

Reputation: 1034

How to make a path configurable?

I am using the following syntax

XDocument config = XDocument.Load(@"path to the xml file");

But i am inlcuding this statement in the c# code.I want to make the path configurable like declare a key in the web.config file of the application with the name path and i should be able to get that in the c# code by xdocument cofnig =xdocument.Load(path).

Is it possible this way ?

Upvotes: 1

Views: 901

Answers (2)

Brent Arias
Brent Arias

Reputation: 30195

Perhaps it would be sufficient to do the usual:

const string key="xmlPath";
...    
string path = ConfigurationManager.AppSettings[key];
XDocument config = XDocument.Load(path);     

This assumes an web.config which contains:

<appSettings>
  <add key="xmlPath" value="c:\path\to\xml\file.xml" />
</appSettings>

Upvotes: 5

e36M3
e36M3

Reputation: 6022

I'm assuming you're asking how to put a key/value pair into the web.config and later retrieve them in the code. Please take a look at the following:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

Upvotes: 1

Related Questions