【发布时间】:2016-07-22 07:47:29
【问题描述】:
我正在尝试将 XDocument 对象作为 .xml 文件保存到磁盘。该对象在调试器中似乎格式良好,我只是在保存实际文件时遇到问题。这是我正在做的事情:
//ofx is an `XElement` containing many sub-elements of `XElement` type
XDocument document = new XDocument(ofx);
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
//destinationPath is a string containing a path, like this
// e.g : "C:\\Users\\Myself\\Desktop"
using (var stream = File.Create(destionationPath + @"export.xml"))
{
List<byte[]> header = new List<byte[]>();
byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
List<string> headers = new List<string>();
headers.Add("foo");
// Just adding some headers here on the top of the file.
// I'm pretty sure this is irrelevant for my problem
foreach (string item in headers)
{
header.Add(Encoding.ASCII.GetBytes(item));
header.Add(newline);
}
header.Add(newline);
byte[] bytes = header.SelectMany(a => a).ToArray();
stream.Write(bytes, 0, bytes.Length);
using (var writer = XmlWriter.Create(stream, settings))
{
document.Save(writer);
}
}
每当我调用它时,目标目录中都没有文件。
注意:目录和文件名也是格式正确的。这在代码中得到了验证。
【问题讨论】: