C#XDocument.Load找不到文件?

时间:2017-11-25 08:43:29

标签: c#

我在控制器中有以下逻辑

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

我收到以下错误

  

System.IO.FileNotFoundException:&#39;找不到文件&#39; C:\ Program Files(x86)\ IIS Express \ makes.xml&#39;。&#39;

我在这里有所需的文件: enter image description here

如何让它传递到加载函数?

1 个答案:

答案 0 :(得分:3)

使用 Server.MapPath 获取文件的路径

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