It's a very simple problem that I have. I use XDocument to generate an XML file. I then want to return it as a XmlDocument class. And I have an XmlDocument variable which I need to convert back to XDocument to append more nodes.
So, what is the most efficient method to convert XML between XDocument and XmlDocument? (Without using any temporary storage in a file.)
You can use the built in xDocument.CreateReader() and an XmlNodeReader to convert back and forth.
Putting that into an Extension method to make it easier to work with.
Sources:
Usage of this Extention are than done simply using something like this
If you need to convert the instance of System.Xml.Linq.XDocument into the instance of the System.Xml.XmlDocument this extension method will help you to do not lose the XML declaration in the resulting XmlDocument instance:
Hope that helps!
You could try writing the XDocument to an XmlWriter piped to an XmlReader for an XmlDocument.
If I understand the concepts properly, a direct conversion is not possible (the internal structure is different / simplified with XDocument). But then, I might be wrong...
There is a discussion on http://blogs.msdn.com/marcelolr/archive/2009/03/13/fast-way-to-convert-xmldocument-into-xdocument.aspx
It seems that reading an XDocument via an XmlNodeReader is the fastest method. See the blog for more details.
For me this single line solution works very well