Question

When I try to load some XML file, with next code:

XDocument configuration = 
    XDocument.Load("%NAME_OF_THE_PATH%/Default_TestRunConfiguration.xml");

It doesn't seams to work, it cannot parse the specific environment variable that I have specified. When I paste that variable into explorer, it works. How can I make this work?

Was it helpful?

Solution

Try this instead:

string path = Environment.ExpandEnvironmentVariables("%NAME_OF_THE_PATH%/Default_TestRunConfiguration.xml");

XDocument configuration = XDocument.Load(path);

This will work with multiple environment variables, and avoids you having to handle them specially. It means you can use the same sort of string as you would in a batch file.

OTHER TIPS

Use Environment.GetEnvironmentVariable, then Path.Combine to safely combine the path and file name.

var dir = Environment.GetEnvironmentVariable("someVariableWithYourPath");

XDocument configuration =
    XDocument.Load(Path.Combine(dir, "Default_TestRunConfiguration.xml"));

Currently, you pass the literal string "%NAME_OF_THE_PATH%/Default_TestRunConfiguration.xml" to the method, and it doesn't know the first part represents an environment variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top