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.
You can wrap the XmlDocument with an XmlNodeReader and feed it to XElement. Load(). The other direction is available as well using XElement. CreateReader().
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.
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.
Just pass the XElement
to the constructor of XDocument
:
var xdoc = new XDocument(new XElement("a", "b"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With