Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert XElement to XDocument

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.

like image 395
atikot Avatar asked Jul 06 '14 15:07

atikot


People also ask

How do I convert XDocument to XElement?

You can wrap the XmlDocument with an XmlNodeReader and feed it to XElement. Load(). The other direction is available as well using XElement. CreateReader().

What is XElement?

The XElement class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: Create elements. Change the content of the element.

What is XDocument C#?

The XDocument class contains the information necessary for a valid XML document, which includes an XML declaration, processing instructions, and comments. You only have to create XDocument objects if you require the specific functionality provided by the XDocument class.


1 Answers

Just pass the XElement to the constructor of XDocument:

var xdoc = new XDocument(new XElement("a", "b")); 
like image 78
EZI Avatar answered Sep 23 '22 00:09

EZI