How to add an existing Xml string into a XElement

r00kie picture r00kie · Sep 12, 2009 · Viewed 11.9k times · Source

How to add an existing Xml string into a XElement?

This code

        var doc = new XDocument(
            new XElement("results", "<result>...</result>")
        );

of course produces this

  <results>&lt;result&gt;&lt;/result&gt;</results>

but I need this

  <results><result>...</result></results>

Any ideas?

Answer

Sani Singh Huttunen picture Sani Singh Huttunen · Sep 12, 2009

This should work:

var xmlString = "<result>sometext</result>";
var xDoc = new XDocument(new XElement("results", XElement.Parse(xmlString)));