C # Remove HTML tags

Source: Internet
Author: User

Remove the HTML tag from a text segment to remove the style and paragraph contained in it. The most common method may be regular expressions. However, you must note that regular expressions cannot process all HTML documents. Therefore, it is better to use an iterative method, such as for loop. See the following code:

                 StripTagsRegex( Regex.Replace(source, ,            Regex _htmlRegex =  Regex(             StripTagsRegexCompiled( _htmlRegex.Replace(source,              StripTagsCharArray([] array =   arrayIndex =  inside =  ( i = ; i < source.Length; i++ let = (let == =  (let == =  (!=++  (array, 

The Code provides two different methods to remove the HTML tag from a given string. One is to use a regular expression and the other is to use a character array for processing in the for loop. Let's take a look at the test results:

   html =  +            

The output result is as follows:

 

In the above Code, three different methods in the HtmlRemoval class are called respectively, and the same result is returned, that is, the HTML tag in the given string is removed. The second method is recommended, that is, to directly reference a pre-defined RegexOptions. Compiled regular expression object, which is faster than the first method. However, RegexOptions. Compiled has some disadvantages. In some cases, its startup time may increase by dozens of times. For details, refer to the following two articles:

RegexOption. Compiled

Regex Performance

Generally, the execution efficiency of regular expressions is not the highest, so another method is given in the HtmlRemoval class to process strings using character arrays. The test program provides 1000 HTML files, each of which contains about 8000 characters. readAllText is used to read data. The test result shows that the execution speed of character arrays is the fastest.

Performance test for HTML removalHtmlRemoval. StripTagsRegex: HtmlRemoval. StripTagsRegexCompiled: HtmlRemoval. StripTagsCharArray: 287 MS [fastest]
File length test for HTML removalFile length before: HtmlRemoval. StripTagsRegex: HtmlRemoval. StripTagsRegexCompiled: HtmlRemoval. StripTagsCharArray:

Therefore, using character arrays to process large volumes of files can save time. In the character array method, only non-HTML characters are added to the Array Buffer. To improve efficiency, it uses character arrays and a new string constructor to receive character arrays and ranges, which is faster than using StringBuilder.

For self-disabled HTML tags

In XHTML, some tags do not have independent close tags, such as <br/> and . The above code should be able to correctly process self-disabled HTML tags. The following are some supported HTML tags. Note that the regular expression method may not be able to correctly handle invalid HTML tags.

Supported tags<br /><br/>< div ><!-- -->

Annotations in HTML documents

The code given in this article may fail to remove the HTML tag from the HTML document comment. In some cases, annotations may contain invalid HTML tags, which are not completely removed during processing. However, scanning these incorrect HTML tags may sometimes be necessary.

How to verify

There are many ways to verify XHTML. We can use the same method as the above Code for iteration. A simple method is to count '<' and '>' to determine whether they match or use a regular expression. Here are some resources to introduce these methods:

HTML Brackets: Validation

Validate XHTML

There are many methods that can be used to remove HTML tags from a given string, and their returned results are also correct. Undoubtedly, using character Arrays for iteration is the most efficient.

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.