2016-11-22 86 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; 
    } 
相關問題