在解决方案中查找文件

时间:2011-11-03 18:26:59

标签: c# mstest

我有一个xml文件说samplexml.xml,我把它存放在一个名为SampleFolder的文件夹中。这里的层次结构如何:

溶液 - >项目 - > SampleFolder-> samplexml.xml。假设我在项目中名为TestClass.cs的类中,如何在字符串中读取xml文件的所有内容。

让我们说,

[TestClass]
public class TestClass
{
   [TestMethod]
   public void TestMethod()
   {
       //TODO: Read all the contents of the xml file in a string.
   }

}

我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试:

string exePath = Path.GetDirectory(Assembly.GetExecutingAssembly().Location);
string xmlPath = Path.Combine(exePath, "SampleFolder");
string fileName = Path.Combine(xmlPath, "SampleXML.xml");

请记住,您应该将编译器设置为将xml文件复制到目标目录