How to replace whole HTMLElement's text to something else?
Here's the example.
webBrowser1.Document.Body.InnerHtml contains:
<img id="image1" src="myImage.gif">
and in my MyWebBrowser class I want to replate whole img tag to some text, for example to string: "<myImage>" (I need it for my chat application, if user doe开发者_StackOverflow社区sn't want to see images)
I thought I could do something like that:
Document.GetElementById("image1").InnerHtml = "<" + Document.GetElementById("image1").GetAttribute("src") + ">";
but it throws exception.
Actually I resolved that by searching those specific tags in whole document and replacing it using String class' methods, but code doesn't look good. If there's more effective way to do that, do not bother answering my question.
You can use the OuterHtml property:
HtmlElement image = Document.GetElementById("image1");
image.OuterHtml = "<" + image.GetAttribute("src") + ">";
Note, however, that assigning to OuterHtml
will invalidate the reference to the element, so image
will not refer to the new content afterwards.
精彩评论