views:

19

answers:

1

I am trying to strip out some HTML tags. I have a project where the person has saved some searches. Problem is the keywords have been highlighted. For example.

<p>Here is some <span class='highlite'>awesome</span> example.</p>

Html Agility turns this into 3 Nodes. A text node, span and text again. I would to create a single tag out of this. So that it looks like

<p>Here is some awesome example.</p>

I tried getting all tags with css class highlite and then

 //Stip all retarded hilite tags
 var hiliteTags = from tags in doc.DocumentNode.SelectNodes("//span[@class='hilite']")
                  select tags;

 foreach (var tag in hiliteTags)
 {
      tag.ParentNode.RemoveChild(tag, true);
 }

but that results in, text node, text node, text node. I was wanting one text node. I then tried to use

Node.InnerText += someVariable;

but InnerText, despite what the documenation says is read only.

Any ideas on how to do this?

Secondly while I am asking, is there a way to get rid of Nodes that contain just text and it is a \r\n. I am not interested in that at all and it just gets in the way and makes the parsing awkward. I would like to be able to remove those too. For example

<tr>
    <td>Foo</td>
    <td>Bar</td>
</tr>

using Html Agility becomes

Node (tr)
Node (\r\n)
Node (td- Foo)
Node (\r\n)
Node (td - Bar)
Node (\r\n)
Node (tr)

I am struggling to select those nodes. I have tried with Linq and I have tried using XPath. I just can't seem to remove them.

A: 

What if you just take the InnerText of the p tag, and create a separate document tree to save it.

var root = HtmlNode.CreateNode("<root></root>");
foreach (var node in doc.DocumentNode.SelectNodes("/p"))
{
    var newNode = HtmlNode.CreateNode(string.Format("<p>{0}</p>", node.InnerText));
    root.AppendChild(newNode);
}

Does this help?

Rohit Agarwal