在ASP .NET Core中,我正在尝试向现有的XML文件中添加一些带属性的XML元素。
在ASP NET 4.5中,我将使用下面的代码来使其工作:
但是在 ASP .NET Core 中,我不能使用 Server.MapPath(),所以我要使用 IHostingEnvironment 来获取完整路径。(点击这里了解更多)
在 ASP .NET Core 上运行下面的完整代码会导致 "无法将String转换为System.IO.Stream" 错误,当尝试运行 "xdoc.Save(pathToDataFile);" 时。
在ASP NET 4.5中,我将使用下面的代码来使其工作:
string path = Server.MapPath("~/Data/foo.xml");
XDocument xdoc = XDocument.Load(path);
//Do stuff with XElement and XAttributes...
xdoc.Save(path);
但是在 ASP .NET Core 中,我不能使用 Server.MapPath(),所以我要使用 IHostingEnvironment 来获取完整路径。(点击这里了解更多)
在 ASP .NET Core 上运行下面的完整代码会导致 "无法将String转换为System.IO.Stream" 错误,当尝试运行 "xdoc.Save(pathToDataFile);" 时。
var contentRoot = hostingEnvironment.ContentRootPath;
string pathToDataFile = contentRoot + "\\Data\\foo.xml";
XDocument xdoc = XDocument.Load(pathToDataFile);
//Do stuff with XElement and XAttributes...
xdoc.Save(pathToDataFile);
为什么 "xdoc.Save()" 在 ASP .NET Core 中不能正常工作,但在 .NET 4.5 中可以工作正常?