正在将 XML 个文件保存到磁盘
Saving XML file to disk
我正在尝试将 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);
}
}
每当我调用它时,目标目录中都没有文件。
注意:目录和文件名也是格式正确的。这在代码中得到了验证。
可能您创建的路径很糟糕。关注你的评论
string destionationPath = "C:\Users\Myself\Desktop";
destionationPath + @"export.xml" // evaluates to: "C:\Users\Myself\Desktopexport.xml"
目录和文件名之间缺少 "\"
。
以下代码对我来说工作正常。
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
Tmp();
}
public static void Tmp()
{
//ofx is an `XElement` containing many sub-elements of `XElement` type
XDocument document = new XDocument(new XElement("myName", "myContent"));
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"
string destionationPath = "D:\Temp\xml_test\";
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);
}
}
}
}
}
为了避免这种情况永远不要自己创建创建路径。使用 Path
class 代替:
Path.Combine(destionationPath, @"export.xml")
我正在尝试将 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);
}
}
每当我调用它时,目标目录中都没有文件。
注意:目录和文件名也是格式正确的。这在代码中得到了验证。
可能您创建的路径很糟糕。关注你的评论
string destionationPath = "C:\Users\Myself\Desktop";
destionationPath + @"export.xml" // evaluates to: "C:\Users\Myself\Desktopexport.xml"
目录和文件名之间缺少 "\"
。
以下代码对我来说工作正常。
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
Tmp();
}
public static void Tmp()
{
//ofx is an `XElement` containing many sub-elements of `XElement` type
XDocument document = new XDocument(new XElement("myName", "myContent"));
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"
string destionationPath = "D:\Temp\xml_test\";
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);
}
}
}
}
}
为了避免这种情况永远不要自己创建创建路径。使用 Path
class 代替:
Path.Combine(destionationPath, @"export.xml")