Is there a way to reuse an XmlReader?
I have a process that uses an XmlReader. I have already done a lot to squeeze maximum performance out of it. So far we have had huge gains from using the Reader as opposed to XmlDoc or DataSet.GetXml(). We expect to g开发者_运维知识库et XML many times a second and I would like to avoid the overhead of recreating the reader every time. I have already cached the XmlReaderSettings but is there anyway to reuse the XmlReader or do I need to recreate it every time?
It might be possible to reuse the XmlReader by using some trick, but you shouldn't. It's intended for one time use only, so it doesn't have any means for restarting itself using a different source.
To try to reuse it would probably be more work than creating a new reader anyway. Creating the reader is rather cheap, and hardly something that would be a performance problem.
This is almost certainly a wasted effort. The XML needs to come from somewhere, a file on disk on a network card. I/O operations are orders of magnitude slower than whatever you'd do to get the reader class instantiated.
The typical trap is that you run a test program to profile the code using the same data over and over again. It is not a realistic test, you'll retrieve the XML from a cached copy, the file system cache for example. That's very fast, it runs at bus speeds. That doesn't work that way in a production machine, the data is almost certainly not yet cached. It needs to be read from the disk for example. That's very slow.
精彩评论