Best way to get InnerXml of an XElement?

Posted by Mike Powell on Stack Overflow See other posts from Stack Overflow or by Mike Powell
Published on 2008-08-06T18:16:55Z Indexed on 2010/03/17 8:51 UTC
Read the original article Hit count: 1447

Filed under:
|
|
|

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()
                };

© Stack Overflow or respective owner

Related posts about .NET

Related posts about Xml