I want to load large XML documents into XDocument objects. A simple synchronous approach using XDocument.Load(path, loadOptions)
works fine, but blocks for an awkwardly long time in the context of the graphical interface when downloading large files (especially from network storage).
I wrote this asynchronous version to improve responsiveness when downloading a document, especially when downloading files over the network.
public static async Task<XDocument> LoadAsync(String path, LoadOptions loadOptions = LoadOptions.PreserveWhitespace)
{
String xml;
using (var stream = File.OpenText(path))
{
xml = await stream.ReadToEndAsync();
}
return XDocument.Parse(xml, loadOptions);
}
However, on a 200 MB boot disk loaded from a local disk, the synchronous version will complete in a few seconds. The asynchronous version (working in a 32-bit context) instead creates OutOfMemoryException
:
at System.Text.StringBuilder.ToString()
at System.IO.StreamReader.<ReadToEndAsyncInternal>d__62.MoveNext()
, , XML XDocument
. XDocument.Load()
String .
? XDocument
- ?