Using XDocument.Load(xmlreader) method?

I heard normally when using XDocument's Load or Parse method the entire file loaded into memory that is why parsing large files with this method is not recommended...but what if I use the following overload to read a xml file

XDocument xml = XDocument.Load(XmlReader.Create(@"C:\OP\file.xml", settings),LoadOptions.None);

Does it still load the entire file into memory, if so, what is this overload good for?

Jon Skeet

Yes, that still loads the whole file's content into an in-memory representation. It's less useful than the XElement.Load(XmlReader) method which can be really useful to load just part of a document into memory at one time.

I'd view the XDocument.Load(XmlReader) method as mostly present for consistency - but I could see it being useful in cases where other APIs provide an XmlReader rather than the raw data. For example, you could have some data structure which provides "fake" XML access by allowing you to create an XmlReader from it. That way it would never need to serialize to the real XML which would then need parsing again.

Another use case would be where you want to use some aspects of XmlReadSettings which aren't avaliable in LoadOptions, such as ignoring comments or using a specific name table.

But no, you shouldn't use XDocument.Load(XmlReader) if you're concerned that the document doesn't fit into memory.


See more on this question at Stackoverflow