Kibi Brat Kibi Brat - 3 months ago 8
HTML Question

Remove HTML tags that contain attribute with given string

I have this HTML code:

<ul>
<li class="purple">Sky</li>
<li class="purple">Earth</li>
<li class="green">Grass</li>
<li class="purple">Sun</li>
</ul>


I want to remove all
<li>
tags with the attribute
class="purple"
.

I haven't found any code similar to this online, so I don't have any idea how to do it.

EDIT:

I've found this code. It finds tags with a given attribute value, but I don't know how could I modify it to remove element.

For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("li")
If Element.GetAttribute("class") = "green" Then
'What to do
End If
Next

Answer

This seems to work:

Dim old = WebBrowser1.Document
    For Each Element As HtmlElement In old.GetElementsByTagName("li")
        If Element.OuterHtml.Contains("green") Then
            Element.OuterHtml = ""
        End If
    Next
WebBrowser1.DocumentText = old.Body.InnerHtml