Today I encountered a really annoying documentation bug in the xml side of .net. Whenever you create an XPathDocument using the Stream based constructor, whenever the construction returns, your stream is closed.
You wouldn’t expect the Stream to be closed, but it is. What is happening behind the scene is that the following constructors wrap whatever you throw at them with an XmlTextReader object, call the Init(XmlReader) method, which itself close the reader, in turn closing the stream.
Only way to go around the problem, create the reader yourself, but don’t close it.
PS: I will respond to IanG comments on my ReaderWriterLock entry with some statistics later this week on when / how to use this pattern (high contention), as I fully agree with him that you shouldn’t just assume without testing. In this specific case, the contention is really high on a very high load long living threads environment. More on that later.