Related to Regular Expressions: C # RichTextBox displays html text content

Source: Internet
Author: User
Tags html interpreter

In the past two days, a function is provided to display the text in html of the captured webpage in richtextbox in the format. However, richtextbox does not recognize html tags. I plan to write an html interpreter myself, but it takes too much time. Since it is a small function, find a shortcut. I thought of two methods.
1. richtextbox is no longer needed and is directly displayed in webbrowser.
Since richtextbox does not recognize html, WebBrowser should always recognize it. Replace richtextbox with WebBrowser.

[Csharp]
String testString = @ "<FONT face = Verdana>
<P> <FONT face = Verdana> test content: </FONT> </P>
<P> <FONT face = Verdana> Haha <BR> dinner <BR> off duty <BR> go home <BR> sleep </FONT> </P>
<P> <FONT face = Verdana> heheh <BR> Shenma </FONT> </P>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT> ";
 
WebBrowser1.DocumentText = testString;
// However, it seems that the insurance should be a little bit more.
WebBrowser1.Document. Write (testString );
 
WebBrowser1.Refresh ();
String testString = @ "<FONT face = Verdana>
<P> <FONT face = Verdana> test content: </FONT> </P>
<P> <FONT face = Verdana> Haha <BR> dinner <BR> off duty <BR> go home <BR> sleep </FONT> </P>
<P> <FONT face = Verdana> heheh <BR> Shenma </FONT> </P>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT> ";

WebBrowser1.DocumentText = testString;
// However, it seems that the insurance should be a little bit more.
WebBrowser1.Document. Write (testString );

WebBrowser1.Refresh ();
2. richtextbox must be displayed for some reasons.
In this case, we have to use a nondescribedomainmethod. Or use WebBrowser.

[Csharp]
String testString = @ "<FONT face = Verdana>
<P> <FONT face = Verdana> test content: </FONT> </P>
<P> <FONT face = Verdana> Haha <BR> dinner <BR> off duty <BR> go home <BR> sleep </FONT> </P>
<P> <FONT face = Verdana> heheh <BR> Shenma </FONT> </P>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT> ";
Using (WebBrowser webBrowser = new WebBrowser ())
{
WebBrowser. Visible = false;
 
WebBrowser. DocumentText = testString;
 
WebBrowser. Document. Write (testString );
 
This. richTextBox1.Text = webBrowser. Document. Body. OuterText;
}
String testString = @ "<FONT face = Verdana>
<P> <FONT face = Verdana> test content: </FONT> </P>
<P> <FONT face = Verdana> Haha <BR> dinner <BR> off duty <BR> go home <BR> sleep </FONT> </P>
<P> <FONT face = Verdana> heheh <BR> Shenma </FONT> </P>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT>
<P> <FONT face = Verdana> <BR> </FONT> </P> </FONT> ";
Using (WebBrowser webBrowser = new WebBrowser ())
{
WebBrowser. Visible = false;

WebBrowser. DocumentText = testString;

WebBrowser. Document. Write (testString );

This. richTextBox1.Text = webBrowser. Document. Body. OuterText;
}
In this way, the plain text content can be obtained for separate processing and application in other places.
The result is as follows:

 

Note:
1. Here webBrowser. Document. Write is used, because webBrowser. DocumentText = testString is null and cannot be used for text;
2. The process is slow;

 

 

From Poplar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.