How to convert XElement to XDocument

atikot picture atikot · Jul 6, 2014 · Viewed 18.8k times · Source

How can I convert XElement into XDocument? Is there some built-in method for this? The only way I can think of is without new XDocument(xelement.ToString()) which will result in creating big strings and then parsing them, thus reducing the performance.

Answer

EZI picture EZI · Jul 6, 2014

Just pass the XElement to the constructor of XDocument:

var xdoc = new XDocument(new XElement("a", "b"));