public void RemoveElementsByTagId(_ELEMENT_TAG_ID tagId, bool onlyIfNoAttributes) { if (tagId == _ELEMENT_TAG_ID.TAGID_NULL) { return; } // Remove the tagId up the parent chain IHTMLElement currentElement = ParentElement(); while (currentElement != null) { if (MarkupServices.GetElementTagId(currentElement) == tagId && (!onlyIfNoAttributes || !HTMLElementHelper.HasMeaningfulAttributes(currentElement))) { try { MarkupServices.RemoveElement(currentElement); } catch (COMException e) { Trace.Fail(String.Format("Failed to remove element ({0}) with error: {1}", currentElement.outerHTML, // {0} e // {1} )); } } currentElement = currentElement.parentElement; } // Remove any other instances IHTMLElement[] elements = GetElements(ElementFilters.CreateTagIdFilter(MarkupServices.GetNameForTagId(tagId)), false); foreach (IHTMLElement e in elements) { if (MarkupServices.GetElementTagId(e) == tagId && (!onlyIfNoAttributes || !HTMLElementHelper.HasMeaningfulAttributes(e))) { try { MarkupServices.RemoveElement(e); } catch (COMException ex) { Trace.Fail(String.Format("Failed to remove element ({0}) with error: {1}", e.outerHTML, // {0} ex // {1} )); } } } }