1

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>

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

While I'd still recommend the accepted solution, same thing can be done using plain regex

string search = "Search Integration";

string pattern = "<tr(?:(?!/?<tr).)*" + search + "(?:(?!/?tr).)*/tr>";
Regex r = new Regex(pattern, RegexOptions.Singleline);
string result = r.Replace(text, "");

Demo: https://dotnetfiddle.net/OcV6E5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.