I started my trusted browser, and the only way I found was to do this manually, and so I did. But I couldn’t let the thought go away, there had to be an easier way as cloning could be used a lot in LINQ.
I looked through the MSDN documentation, I sure enough I’ve found it, one of the constructors can do this, XDocument(XDocument). There is simply no reason to manual clone the document, and a constructor just like this exists for an XElement.
Many people may know this constructor exists, but I sure didn’t as it doesn’t following the “standard” .NET cloning methods. But the constructor way is much better, as we now are explicitly told whether or not the clone will be a shallow or deep clone.
Update: Added example as requested.
XmlDocument originalXml = new XmlDocument(); XmlDocument xmlCopy = new XmlDocument(originalXml);