E.Meir E.Meir - 3 months ago 10
ASP.NET (C#) Question

Removing a parent tag of a string until his closing tag from Html string

i have a very long string, what i am, trying to accomplish is delete a section in that string from the

<tr>
parent tag of the string to his closing tag
</tr>
. (hope i am clear enough).

So when i call The method RemoveSection with the text "Search Integration"




Html before



<tr>
<td class=\"SectionHeaderHolder\" colspan=\"4\">
<p class=\"SectionHeader\">Header XX<span class=\"help\">Help</span></p>
</td>
</tr>

<tr>
<td class=\"SectionHeaderHolder\" colspan=\"4\">
<p class=\"SectionHeader\">Search Integration<span class=\"help\">Help</span></p>
</td>
</tr>

<tr>
<td class=\"SectionHeaderHolder\" colspan=\"4\">
<p class=\"SectionHeader\">Header YY<span class=\"help\">Help</span></p>
</td>
</tr>






  • The string that Remove function gets will be under
    <p class=\"SectionHeader\">

  • There will be only one section with that string- so the first occurrence should be handled by the remove function.






Html after



<tr>
<td class=\"SectionHeaderHolder\" colspan=\"4\">
<p class=\"SectionHeader\">Header XX<span class=\"help\">Help</span></p>
</td>
</tr>

<tr>
<td class=\"SectionHeaderHolder\" colspan=\"4\">
<p class=\"SectionHeader\">Header YY<span class=\"help\">Help</span></p>
</td>
</tr>

Answer

You could use HtmlAgilityPack for this. A simple LinqPad example:

void Main()
{
    string input = "<tr><td class=\"SectionHeaderHolder\" colspan=\"4\"><p class=\"SectionHeader\">Header XX<span class=\"help\">Help</span></p></td></tr>"
                + "<tr><td class=\"SectionHeaderHolder\" colspan=\"4\">    <p class=\"SectionHeader\">Search Integration<span class=\"help\">Help</span></p>    </td>    </tr>"
                + "<tr><td class=\"SectionHeaderHolder\" colspan=\"4\">    <p class=\"SectionHeader\">Header YY<span class=\"help\">Help</span></p>    </td>    </tr>";

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(input);

    doc.DocumentNode.SelectSingleNode("//p[text()='Search Integration']").ParentNode.ParentNode.Remove();

    string output = doc.DocumentNode.OuterHtml;

    input.Dump();
    output.Dump();
}