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.
Asked
Active
Viewed 2.3k times
34

atikot
- 4,761
- 4
- 26
- 34
-
Why do you need this? – EZI Jul 06 '14 at 15:13
-
I have some code that uses XDocument while i have XElement, don't want to rewrite it – atikot Jul 06 '14 at 15:14
-
i actually tried it, "Task.Run(parser.GetEvent(new XDocument(outXe)));" but saw an error , thought it was because of the conversion, turned out i was missing the ()=> – atikot Jul 06 '14 at 15:17
-
-1 for not marking the answer. – Darek Jul 06 '14 at 15:31
-
3forgot because the stupid 5 minutes rule... – atikot Jul 07 '14 at 19:21
2 Answers
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