Callat
Callat

Reputation: 3044

C# XDocument.Load could not find file?

I have the following logic in a controller

public void Makes() {
            // iterate over makes.xml
            XDocument myDoc = XDocument.Load("makes.xml");
            var make = myDoc.Descendants("make");
            List<string> list = new List<string>();
            foreach (var item in make)
            {
                Console.WriteLine(item);
            }
    }

And I get the following error

System.IO.FileNotFoundException: 'Could not find file 'C:\Program Files (x86)\IIS Express\makes.xml'.'

I have the needed file here: enter image description here

How can I get it to pass into the load function?

Upvotes: 0

Views: 856

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

Use Server.MapPath to get the path to your file

Server.MapPath("~/App_Data/makes.xml")

Upvotes: 3

Related Questions