Add an XNode of varying content to an element



I'm stumped by the need for a little bit of XML gymnastics. Maybe I'm over complicating this.


I'm trying to add a p element to an XML document. The content of the paragraph may be plain text. Or may have elements within it, i.e link tags, styling, etc.


The code I have currently is below:



foreach (var paragraph in paragraphs)
{
doc.Add(new XElement("p",
new XElement("a", new XAttribute("type", "paranum"), new XText(_paragraphCount)),
new XText(paragraph.Content)));
}


When I use this, however, any elements that are in there don't come through as elements, but are escaped, probably because they're in XText.


However, I seem to recall trying XElement.Parse, but it not liking it when there is no root element, which makes sense. Sometimes, there aren't even any elements, just text.


How would I go about putting the content of this paragraph in the p element and preserving child elements, if any? Preferably without wrapping it in another element to allow me to do XElement.Parse.


No comments:

Post a Comment