Best approach to write huge XML data to a file?

I am currently exporting a database table with huge data (100,000+ entries) to an xml file using the XmlTextWriter class, and I am writing directly to a file on a physical disk.

_XmlTextWriterObject = new XmlTextWriter(_xmlFilePath, null);

While my code is working fine, my question is, what is the best approach? Should I instead write all the xml in the memory stream first, and then write the XML document to the physical file from the memory stream? And what are the implications for memory / performance in both cases?

EDIT

Sorry that I could not convey what I wanted to say. Thanks Ash for pointing out. I will actually use XmlTextWriter, but I wanted to say whether to pass the string of the physical file to the XmlTextWriter constructor (or, as John suggested, to the method XmlTextWriter.Create()) or to use api based on the stream. My current code is as follows:

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
}
+3
source share
3 answers

While my code is working fine, my question is, what is the best approach?

As already mentioned, and your update XmlWriter.Createis fine.

Or do you need to first write the entire XML file in the memory stream, and then write the XML document to the physical file from the memory stream?

? , , - , FileStream, .

/ ?

XML . . , . , , FileStream .

+4

, XmlWriter, , XmlDocument ( DOM), .

, XmlWriter IDisposable, :

using (XmlWriter _XmlTextWriterObject = XmlWriter.Create(_xmlFilePath))
{
   // Code to do the write here
}
+7

As John Saunders said, it's best to use XmlWriter.Create (). This is a MSDN recommendation. The XmlWriter.Create () method can also accept an XmlWriterSettings object. There you can customize your behavior a bit. If you don't need validation and character validation, you can turn it off and get a bit more speed. for instance

XmlWriterSettings settings = new XmlWriterSettings();
settings.CheckCharacters = false;
using (XmlWriter writer = XmlWriter.Create("path", settings))
{
    //writing code
    writer.Flush();
}

Otherwise, I think everything is in order.

+2
source

Source: https://habr.com/ru/post/1741893/


All Articles