I'm using XDocument and XElement objects to write a large XML file (currently 8MB). I'd like to switch to XStreamingElement so that I can write the file without having all the data in memory at once. Unfortunately, I need to include an encoding declaration. Can I do that without using an XDocument?
views:
24answers:
1
+2
A:
The trick is to use XmlWriterSettings to specify the encoding when you create the XmlWriter. It generates the declaration automatically.
var doc =
new XStreamingElement("openerp",
LoadReferenceData(),
LoadAccounts(),
LoadPartners(),
LoadComponents());
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.GetEncoding("latin1");
using (var writer = XmlWriter.Create(
outputFilename.Text,
settings))
{
doc.WriteTo(writer);
}
Each of the Load methods returns IEnumerable and uses yield return
to generate XML as it loops through a database query result.
Don Kirkby
2009-11-11 00:52:04