XPathDocument Threading Question

516 Views Asked by At

I have a web application where I am trying to cache an XPathDocument.

The XPathDocument is created as follows.

XPathDocument xdoc = new XPathDocument(new StringReader(ruleXml));

Then I want to just cache this xdoc and retrieve it for each request.

And then I plan to call

XPathNavigator nav = xdoc.CreateNavigator();

on each thread.

My question is whether this is thread safe or not. Can you have multiple XPathNavigator classes on different threads with the same underlying XPathDocument?

If not I will just cache the ruleXml string and create a new XPathDocument on each thread.

Just wondering what peoples suggestions are in a scenario like this where I want to cache an xml read only document and then do different xpath queries on each thread.

1

There are 1 best solutions below

0
On

That should be safe, as long as the document is read-only. The entire purpose of keeping XPathNavigator separate from XmlDocument or XPathDocument is to be able to have multiple navigations going on at the same time, against the same document.