2016-11-22 37 views
0

我有一个用于验证XML文件的测试用例。 当使用VS2010和.Net 3.5框架时,下面的代码工作得很好,我可以加载XML文件。我的文件位置是应用程序的源文件夹。XMLDocument.Load()在运行测试项目时不加载文件

XmlDocument doc = new XmlDocument(); 
     try 
     { 
      doc.Load("Terms_and_Conditions.xml"); 

      XmlNode node; 

      XmlElement root = doc.DocumentElement; 
      //Select and display the value of the element. 
      node = root.SelectSingleNode(NodeSelection); 

      return node.InnerText; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

解决方案文件夹: enter image description here

当我在.net 4.6.1运行相同的代码,文件路径解析到 C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Terms_and_Conditions.xml

任何人有一个想法,为什么这个问题与.net 4.6 .1

+0

https://github.com/nunit/nunit/issues/1072 –

+0

谢谢。上述链接有所帮助。 –

回答

1

发生这种情况是因为您的单元测试运行在测试运行器中,该测试运行器位于Common7文件夹中,而不在您的项目bin文件夹中。由于您指定了xml文件的相对路径,因此程序将在当前文件夹(Common7文件夹)中查找该文件。

0

感谢大家的快速帮助。

以下是我如何解决上述链接的问题。

XmlDocument doc = new XmlDocument(); 
    try 
    { 


     var path = System.IO.Directory.GetParent(System.IO.Directory.GetParent(TestContext.CurrentContext.TestDirectory).ToString()); 

     doc.Load("Terms_and_Conditions.xml"); 

     XmlNode node; 

     XmlElement root = doc.DocumentElement; 
     //Select and display the value of the element. 
     node = root.SelectSingleNode(NodeSelection); 

     return node.InnerText; 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
相关问题