I'm trying to create a new XDocument as follows:
var xmlString = _documentDictionary[documentKey].ToString(SaveOptions.DisableFormatting);
XDocument xml = XDocument.Parse(xmlString);
I now have xml
which I would have though was a stand-alone instance of a document because I extracted the string from the original document and created a new one from that.
But when I modify xml
and then inspect the _documentDictionary[documentKey]
I can see that the original document has been modified also.
How can I get a new independent document from the existing collection that I have?
Note:
I've tried these but it doesn't work:
var xmlString = _documentDictionary[documentKey].ToString(SaveOptions.DisableFormatting);
var copyDoc = new XDocument(xmlString);
and
var copyDoc = new XDocument(_documentDictionary[documentKey]);
Try to copy constructor, like;
From MSDN:
There is a copy constructor defined for
XDocument
class:Quick test
name
is"Test2"
andname2
is"Test"
, what proofs that changes made ondoc
don't affectdoc2
.