简体   繁体   中英

XMLDocument VS XmlReader which is more efficient to validate well-formed XML in C#

In other words, is XMLDocument more efficient than XmlReader to validate a well formed xml document? from a memory prospective if you have a large XML file and used XMLDocument you might end with out of memory exception.

That is the answer I was looking for it was provided in a comment by @canton7 XmlReader is a low-level streaming API. XmlDocument is a higher-level API built on top of XmlReader. Using the level which is appropriate: maybe you want an easier-to-use convenient API, or maybe you want to write lots of code to use the more efficient streaming API

To expand on your answer a bit, it depends on what you are trying to do. If this is truly just validating, then XmlDocument is probably the correct version, as it has to complete load to validate. The reader is a "firehose cursor" of sorts that streams the data as you read it. When you have a reader, reading to end will validate. May be a bit faster, but a bit more code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM