I'm trying to simply load a very large string from an Xml file and save it to a temporary file inside the IXmlSerializable#ReadXml method, the code being used is below.
The problem is I'm getting an OutOfMemoryException on the reader.ReadStartElement("data"); line. It appears that the XmlReader is attempting to preload the value string and as it is ~500Mb in this case it is failing to allocate a StringBuilder for it.
Is there some better way of copying this string into a file, or some way to bypass the preloading of the XmlReader?
public void ReadXml(XmlReader reader)
{
// Read other elements
reader.ReadStartElement("data");
this.dataFile = Path.GetTempFileName();
FileStream tempFile = File.Create(this.dataFile);
char[] buffer = new char[CHUNK_SIZE];
int count;
using (StreamWriter writer = new StreamWriter(tempFile))
{
while ((count = reader.ReadValueChunk(buffer, 0, CHUNK_SIZE)) != 0)
{
writer.Write(buffer, 0, count);
}
}
reader.ReadEndElement();
}