Using the XDocument.Load (xmlreader) method?

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

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

It still loads the entire file into memory, if so, then why is this overload good?

+5
source share
1 answer

Yes, it still loads the contents of the entire file into a view in memory. This is less useful than the XElement.Load(XmlReader) method, which can be really useful for loading only part of a document into memory at a time.

I would consider the XDocument.Load(XmlReader) method, as usually present for consistency, but I could see that it is useful in cases where other APIs provide the XmlReader , rather than the raw data. For example, you may have some data structure that provides "fake" XML access, allowing you to create an XmlReader . This way, he will never need to serialize real XML, which then needs to be parsed.

Another use case is that you want to use some aspects of XmlReadSettings that are not available in LoadOptions , such as ignoring comments or using a specific name table.

But no, you should not use XDocument.Load(XmlReader) if you are worried that the document does not fit into memory.

+5
source

Source: https://habr.com/ru/post/1275661/


All Articles