Search code examples
c#xmlstreamout-of-memoryxmlreader

Why is my new XmlTextReader(stream) reading in many megabytes into memory rather than streaming properly?


I am getting Out of Memory Exceptions when STREAMING in XML into an XmlReader! Looking in a memory profiler we can see that it is calling StringBuilder.Append over and over resulting in tons of 128KB buffers filling all of memory.

That's pretty contrary to "streaming". It shouldn't be loading more than one 4KB buffer.


Solution

  • Reading through the .NET source code, it turns out there's a "v1compat" mode that will indeed read way ahead, defeating the purpose of streaming. So, how do you avoid getting it into that stupid mode?

    It turns out that there's a HUGE difference between calling 'new XmlTextReader(stream)' and 'XmlReader.Create(stream)' that Microsoft didn't bother to document... and I could never find in any post anywhere... the former puts it into 'v1compat' mode!!!

    Sooo, unless you need your XmlReader to behave exactly like it did in .NET 1.1, including improper streaming behavior, you should NEVER EVER call 'new XmlTextReader(stream)' ... instead use 'XmlReader.Create(stream)' or one of the variants that takes an XmlReaderSettings if you need to try to match the settings XmlTextReader used (if you don't pass an XmlReaderSettings, then at least some of the settings will be different... I am not sure what settings would best match 'new XmlTextReader'... if anybody knows, please add that here!