我目前正在使用XmlTextWriter 类将包含大量数据(100000+ 条记录)的数据库表导出到 xml 文件中,并且我正在直接写入物理驱动器上的文件。
_XmlTextWriterObject = new XmlTextWriter(_xmlFilePath, null);
虽然我的代码运行正常,但我的问题是这是最好的方法吗?我是否应该先将整个 xml 写入内存流,然后再将 xml 文档从内存流写入物理文件?在这两种情况下对内存/性能有什么影响?
编辑
抱歉,我实际上无法传达我的意思。感谢 Ash 指出。我确实会使用 XmlTextWriter 但我的意思是说是将物理文件路径字符串传递给 XmlTextWriter 构造函数(或者,如约翰建议的那样,传递给XmlTextWriter.Create()
方法)还是使用基于流的 api。我当前的代码如下所示:
XmlWriter objXmlWriter = XmlTextWriter.Create(new BufferedStream(new FileStream(@"C:\test.xml", FileMode.Create, System.Security.AccessControl.FileSystemRights.Write, FileShare.None, 1024, FileOptions.SequentialScan)), new XmlWriterSettings { Encoding = Encoding.Unicode, Indent = true, CloseOutput = true });
using (objXmlWriter)
{
//writing xml contents here
}