我正在尝试加载一个XML文件,它位于我的项目文件夹中(使用Visual Studio 2012)。
结构如下:
solutionRoot\
- service\
-- ServiceClass.cs
-- AppValues.xml <-- this is the file I want to load
在我的ServiceClass中,我正在尝试使用以下代码从XML文件中读取:
public String GetXmlElement(String elementName)
{
[....]
XDocument document = XDocument.Load(@"\service\AppValues.xml");
[...]
}
当我尝试测试代码时,出现以下错误:
Test method PandaTests.ServiceTest.ReadXmlCanReadXml threw exception:
System.IO.DirectoryNotFoundException: Could not find a part of the path
'C:\Users\MyName\Documents\GitHub\project\Project22\PandaTests\bin\Debug\service\AppValues.xml'.
显然我的问题出在路径上,但我无法弄清如何正确获取相对路径。我已经查看了stackoverflow上的其他问题,但其中许多问题似乎过于复杂。有没有一种简单的方法可以加载XML文件而不必给出绝对路径?