I’m trying to serialize a very large IEnumerable<MyObject> using an XmlSerializer without keeping all the objects in memory.
The IEnumerable<MyObject> is actually lazy..
I’m looking for a streaming solution that will:
- Take an object from the
IEnumerable<MyObject>
Serialize it to the underlying stream using the standard serialization (I don’t want to handcraft the XML here!) - Discard the in memory data and move to the next
I’m trying with this code:
using (var writer = new StreamWriter(filePath))
{
var xmlSerializer = new XmlSerializer(typeof(MyObject));
foreach (var myObject in myObjectsIEnumerable)
{
xmlSerializer.Serialize(writer, myObject);
}
}
but I’m getting multiple XML headers and I cannot specify a root tag <MyObjects> so my XML is invalid.
Any idea?
Thanks
The
XmlWriterclass is a fast streaming API for XML generation. It is rather low-level, MSDN has an article on instantiating a validating XmlWriter usingXmlWriter.Create().Edit: link fixed. Here is sample code from the article: