1

I already searched a long time but I didnt find an answer. I hope you can help me. So I want to remove one duplicate HTML tag for example:

I have a text like this: <BIG><BIG><BIG>Text</BIG></BIG></BIG>

and if I press a button it should be something like this: <BIG><BIG>Text</BIG></BIG>

I already tried RegExbut it will delete all HTML Tags. I also found the HTML Agility Pack but I dont know how to do it.

The RegEx I tried:

public static string RemoveHTML(string source)
{
return Regex.Replace(source, "<.*?>", string.Empty);
}
nicoh
  • 374
  • 2
  • 12

1 Answers1

1

Here how to do it with HtmlAgility pack. Full example in dotNetFiddle

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<BIG><BIG><BIG>Text</BIG></BIG></BIG>");

var result = doc.DocumentNode.Descendants("BIG").FirstOrDefault();

if(result != null)
{
    doc.DocumentNode.RemoveChild(result, true);
}

This should be enough. You are finding the first element big and remove it from the document without removing his children(this is done by true parameter in RemoveChild)

mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • @nicoh ask one question per post this is policy with SO. You can ask more detail question and what exactly is the problem now with already my solution. I don't know what you to do in this case and what code return this. Probably you don't have any childs and you are trying to remove it. – mybirthname Dec 19 '16 at 14:08