简体   繁体   English

ASP .NET Core写入XML文件

[英]ASP .NET Core write to XML-file

In ASP .NET Core, I am trying to add some XML-Element with attributes to an existing XML file. 在ASP .NET Core中,我尝试将一些带有属性的XML-Element添加到现有的XML文件中。

In ASP NET 4.5, I would have used the code below to make this working: 在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);

But with ASP .NET Core, I cannot use Server.MapPath(), So I get the complete path with IHostingEnvironment instead: (Read more here ) 但是使用ASP .NET Core,我无法使用Server.MapPath(),所以我得到了IHostingEnvironment的完整路径:( 在这里阅读更多内容)

Running the complete code below on ASP .NET Core will result in "Cannot convert from String to System.IO.Stream" when trying to run "xdoc.Save(pathToDataFile);" 在ASP .NET Core上运行下面的完整代码将导致在尝试运行“xdoc.Save(pathToDataFile);”时“无法从String转换为System.IO.Stream” ?? ??

  var contentRoot = hostingEnvironment.ContentRootPath;
  string pathToDataFile = contentRoot + "\\Data\\foo.xml";
  XDocument xdoc = XDocument.Load(pathToDataFile);
    //Do stuff with XElement and XAttributes...
  xdoc.Save(pathToDataFile);

Why is "xdoc.Save()" not working in ASP .NET Core but working fine in .NET 4.5? 为什么“xdoc.Save()”在ASP .NET Core中不起作用,但在.NET 4.5中运行良好?

APIs available in .NET Core are a subset of the ones available in the full .NET framework. .NET Core中提供的API是完整.NET框架中可用的API的一部分。 In some areas, you'll find that pretty much everything from .NET 4.5 is available in .NET Core, but that's not always the case. 在某些领域,您会发现几乎所有.NET 4.5中都可以使用.NET Core,但情况并非总是如此。

In your case, if you have a look with Visual Studio at what overloads of the Save method are available, you'll find these ones: 在您的情况下,如果您在Visual Studio中查看Save方法的重载是否可用,您将找到以下内容:

public void Save(Stream stream);
public void Save(TextWriter textWriter);
public void Save(XmlWriter writer);
public void Save(Stream stream, SaveOptions options);
public void Save(TextWriter textWriter, SaveOptions options);

The reason why you have a compilation error is now pretty clear. 您有编译错误的原因现在非常清楚。 In .NET Core, there's no overload accepting a string that defines the file path where the document should be saved. 在.NET Core中,没有重载接受一个string来定义应该保存文档的文件路径。

You'll have to create a write-enabled Stream pointing to the desired path first and pass that Stream to the Save method. 您必须首先创建一个指向所需路径的启用写入的Stream ,然后将该Stream递给Save方法。 You can have a look at the full .NET framework implementation for reference. 您可以查看完整的.NET框架实现以供参考。

I had the same issue, and FileStream works for me. 我有同样的问题,FileStream适合我。

FileStream fileStream = new FileStream("file.xml", FileMode.Create);
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true};
XmlWriter writer = XmlWriter.Create(fileStream, settings);

Remember to use the following lines of code to prevent the file from being truncated. 请记住使用以下代码行来防止文件被截断。

writer.Flush();
fileStream.Flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM