C # Issues needing attention in HTML translation

Source: Internet
Author: User

Original: C # issues needing attention in HTML translation

In the B/S program, we will use a little bit of HTML special symbol translation. such as: "&"-"&amp;", "<"-"&lt;", ">"-"&gt;", "" "-" &nbsp; "...

Symbol translation becomes code:

        /// <summary>        ///htmls the en code. /// </summary>        /// <param name= "SHTML" >The HTML.</param>        /// <returns></returns>         Public Static stringHTMLEnCode (stringSHTML) {            stringStemp =""; if(Shtml.length = =0)            {                return ""; } stemp= Shtml.replace ("<","&lt;"); Stemp= Stemp.replace ("&","&amp;"); Stemp= Stemp.replace (">","&gt;"); Stemp= Stemp.replace ("'","& #39;"); Stemp= Stemp.replace (" ","&nbsp;"); Stemp= Stemp.replace ("\"","&quot;"); Stemp= Stemp.replace ("\ n","<br/>"); returnstemp; }

We use it when we need to convert it into HTML code:

 /// <summary>        ///htmls the de code. /// </summary>        /// <param name= "SHTML" >The HTML.</param>        /// <returns></returns>         Public Static stringHtmlDecode (stringSHTML) {            stringStemp =""; if(Shtml.length = =0)            {                return ""; } stemp= Shtml.replace ("&lt;","<"); Stemp= Stemp.replace ("&gt;",">"); Stemp= Stemp.replace ("& #39;","'"); Stemp= Stemp.replace ("&nbsp;"," "); Stemp= Stemp.replace ("&quot;","\""); stemp = Stemp.replace ("&amp; ", "&  "); Stemp= Stemp.replace ("<br/>","\ n"); returnstemp; }

But we tend to overlook the most important point is that when we convert the translation into HTML, we need to pay attention to the order of transcoding, we have to "<", ">", "" "" "", "\" After the translation of the symbol of the final conversion "&" character, otherwise, the translated &am p; If followed by "nbsp;" Such consecutive characters will be converted to a space.

Do not accumulate kuibu, not even thousands of miles, no small stream, no to become Jianghai. Just accumulate, hope this discovery can also help you!

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.