Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create XElement from a string?

Tags:

Say I have a string like

string var = "This is a test";

Then I want to use this string to create an instance of XElement like:

XElement element =  XElement.Load(var);
like image 222
KentZhou Avatar asked Oct 14 '10 17:10

KentZhou


People also ask

How to create new XElement in c#?

var el = new XElement("name", value);

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.


1 Answers

Use the XDocument.Parse method to parse the string into an XML Document.

var document = XDocument.Parse("<element>This is a test</element>");

var element = document.Root;
like image 56
Michael S. Scherotter Avatar answered Oct 18 '22 16:10

Michael S. Scherotter