Cloning an XmlDocument doesn’t make the copy thread safe


This could be a gotcha, but cloning an XmlDocument doesn't quite make a completely independent copy.


The CloneNode method is intended in general to be a way of creating copies of XML nodes under the same XmlDocument scope / owner XmlDocument. Cloning the XmlDocument does create a copy, but it shares the same scope, which has pretty much shrank to the shared NameTable / implementation at that point.


So you can write a thread-safe implementation of NameTable and use that in the constructor of the original document, or you create a new document and then ImportNode, which will create a distinct node under the new document.


Enjoy!

Comments (0)

Skip to main content