I have a string:
string hmtl = "<DIV><B> xpto </B></DIV>
and need to remove the tags of <div>
and </DIV>
. With a result of : <B> xpto </B>
Just <DIV> and </DIV>
without the removal of a lot of html tags, but save the <B> xpto </B>
.
Use htmlagilitypack
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<html>yourHtml</html>");
foreach(var item in doc.DocumentNode.SelectNodes("//div"))// "//div" is a xpath which means select div nodes that are anywhere in the html
{
item.InnerHtml;//your div content
}
If you want only B tags..
foreach(var item in doc.DocumentNode.SelectNodes("//B"))
{
item.OuterHtml;//your B tag and its content
}
If you are just removing div tags, this will get div
tags as well as any attributes they may have.
var html =
"<DIV><B> xpto <div text='abc'/></B></DIV><b>Other text <div>test</div>"
var pattern = "@"(\</?DIV(.*?)/?\>)"";
// Replace any match with nothing/empty string
Regex.Replace(html, pattern, string.Empty, RegexOptions.IgnoreCase);
Result
<B> xpto </B><b>Other text test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With