34

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.

atikot
  • 4,761
  • 4
  • 26
  • 34

2 Answers2

53

Just pass the XElement to the constructor of XDocument:

var xdoc = new XDocument(new XElement("a", "b"));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
EZI
  • 15,209
  • 2
  • 27
  • 33
4

I've had great success with this:

var xDoc = XDocument.Load(xml.CreateReader());

rorpage
  • 41
  • 2