Mubashir Koul
Mubashir Koul

Reputation: 171

XMLDocument.Load() Does not load the file while running test project

I have a test case which validates an XML file. When using VS2010 with .Net 3.5 framework, the below code works perfectly fine, i am able to load the XML file. My file is location is Source folder of the application.

 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;
        }

Solution Folder: enter image description here

When I run the same code in .Net 4.6.1, file path resolves to C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Terms_and_Conditions.xml

Anyone have an idea why this issue with .Net 4.6.1

Upvotes: 0

Views: 1399

Answers (2)

Mubashir Koul
Mubashir Koul

Reputation: 171

Thanks everyone for the quick help.

Here is how I solved the problem with above link.

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;
}

Upvotes: 0

yadejo
yadejo

Reputation: 1968

This happens because your unit tests run in the test runner, which is located in the Common7 folder and not in your projects bin folder. Since you specify a relative path to the xml file, the program will look for the file in the current folder, which is the Common7 folder.

Upvotes: 1

Related Questions