How to convert XElement to XDocument

20,793

Solution 1

Just pass the XElement to the constructor of XDocument:

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

Solution 2

I've had great success with this:

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

Share:
20,793
atikot
Author by

atikot

Updated on September 13, 2020

Comments

  • atikot
    atikot over 3 years

    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.

  • Darek
    Darek almost 10 years
    If it is a new XElement, you could also grab the Document property.
  • John Saunders
    John Saunders almost 10 years
    Is XElement.Document always non-null?