Is reading from an XmlDocument object stream safe?

I was wondering if I can safely read from an XmlDocument object using SelectNodes () and SelectSingleNode () from multiple threads without problems. MSDN says that they do not guarantee thread safety. If SelectNodes () and SelectSingleNode () represent problems running from multiple threads, can I use the correct lock to avoid any problems? I have a WCF service that needs to grab a piece of xml from a database and select some information from this xml. I would like to cache xml so that it doesn't get into the database so often, but I'm worried about thread safety and performance. Is there a better way to do this? Thanks

+3
source share
4 answers

Here's the deal. If the documentation indicates that the instance methods are not protected threads, then it is best to take note. And if you decide to use the class in a multi-threaded script without proper synchronization mechanisms, then you need to 1) be aware of the consequences of ignoring the documentation and 2) prepare for all your assumptions in order to be invalid in future versions of the class.This advice is valid even for methods that, apparently only read the internal state.

, SelectNodes SelectSingleNodes ? , , ! Reflector, , , . , , ?

, , SelectNodes SelectSingleNodes , , , .

  • XmlDocument , SelectNodes SelectSingleNode, ... -. XmlDocument, , , , , , , .
  • XmlDocument , SelectNodes SelectSingleNodes . , , , . , .

... .

+5

/ / XML, , . ( ?), ReaderWriterLockSlim , .

+2

SelectNodes/SelectSingleNode ( ). , , xml.

0

you can also use the MsXml FreeThreadedDOMDocument model instead of the classic DomDocument when you call createInstance.

Beware that according to this article , FreeThreadedDOMDocument is 7x or 10 times slower than classic DomDocument.

0
source

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


All Articles