What's the best way to get the contents of the mixed body
element in the code below? The element might contain either XHTML or text, but I just want its contents in string form. The XmlElement
type has the InnerXml
property which is exactly what I'm after.
The code as written almost does what I want, but includes the surrounding <body>
...</body>
element, which I don't want.
XDocument doc = XDocument.Load(new StreamReader(s));
var templates = from t in doc.Descendants("template")
where t.Attribute("name").Value == templateName
select new
{
Subject = t.Element("subject").Value,
Body = t.Element("body").ToString()
};
// using Regex might be faster to simply trim the begin and end element tag
you know? the best thing to do is to back to CDATA :( im looking at solutions here but i think CDATA is by far the simplest and cheapest, not the most convenient to develop with tho
How about using this "extension" method on XElement? worked for me !
OR use a little bit of Linq
Note: The code above has to use
element.Nodes()
as opposed toelement.Elements()
. Very important thing to remember the difference between the two.element.Nodes()
gives you everything likeXText
,XAttribute
etc, butXElement
only an Element.