How to convert from string to XElement object

realn picture realn · Oct 21, 2011 · Viewed 33.9k times · Source

I have a string like this: "<Root><Child>Hey</Child></Root>"

How can I convert this to an XElement Object?

Answer

Use XElement.Parse method like below

XElement xmlTree = XElement.Parse("<Root><Child>Hey</Child></Root>");
Console.WriteLine(xmlTree);