tags:

views:

144

answers:

2

I'm extracting XML node from an XElement. When I use XElement.Value it strips any HTML that may be in the node.

I know that if I do XElement.ToString() I can keep the HTML, but it also gives me the node tags. Is there any way to extract the content of a Node as is without the HTML being stripped out?

Cheers.

+1  A: 

You need to concatenate the nodes inside the XElement, like this:

node.Nodes().Aggregate(new StringBuilder(), (sb, n) => sb.Append(n.ToString())).ToString()

Or, in .Net 4.0:

String.Concat(node.Nodes())
SLaks
This worked perfectly. Makes total sense, when you see it.Cheers
Arnej65
+1  A: 

Alternatively:

using System.Xml.XPath;

string xml = node.CreateNavigator().InnerXml;
Pavel Minaev