当前位置:编程学习 > asp >>

.NET输出紧凑HTML

好代码,当然要保留呀。呵呵

 

如图:

\

 

 

方法一:

 

 

private static readonly Regex REGEX_LINE_BREAKS = new Regex(@"\n\s*", RegexOptions.Compiled); 

private static readonly Regex REGEX_LINE_SPACE = new Regex(@"\n\s*\r", RegexOptions.Compiled); 

private static readonly Regex REGEX_SPACE = new Regex(@"( )+", RegexOptions.Compiled); 

protected override void Render(HtmlTextWriter writer) 

    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter())) 

    { 

        base.Render(htmlwriter); 

        string html = htmlwriter.InnerWriter.ToString(); 

        html = REGEX_LINE_BREAKS.Replace(html, string.Empty); 

        html = REGEX_LINE_SPACE.Replace(html, string.Empty); 

        html = REGEX_SPACE.Replace(html, " "); 

        writer.Write(html.Trim()); 

    } 

 

方法二:

 

 

private static readonly Regex regReplaceBlank = new Regex(">(\\s+)<", RegexOptions.IgnoreCase); 

   private static readonly Regex regReplaceLine = new Regex("\\s\\n", RegexOptions.IgnoreCase); 

 

   protected override void Render(HtmlTextWriter writer) 

   { 

       using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter())) 

       { 

           base.Render(htmlwriter); 

           string html = htmlwriter.InnerWriter.ToString(); 

 

           html = regReplaceBlank.Replace(regReplaceLine.Replace(html, ""), "><"); 

           writer.Write(html.Trim()); 

       } 

   } 

 

方法三:www.zzzyk.com

 

 

private static readonly Regex REGEX_LINE_BREAKS = new Regex(@"\n\s*", RegexOptions.Compiled); 

private static readonly Regex REGEX_LINE_SPACE = new Regex(@"\n\s*\r", RegexOptions.Compiled); 

private static readonly Regex REGEX_SPACE = new Regex(@"( )+", RegexOptions.Compiled); 

 

private static readonly Regex regReplaceBlank = new Regex(">(\\s+)<", RegexOptions.IgnoreCase); 

private static readonly Regex regReplaceLine = new Regex("\\s\\n", RegexOptions.IgnoreCase); 

 

protected override void Render(HtmlTextWriter writer) 

    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter())) 

    { 

        base.Render(htmlwriter); 

        string html = htmlwriter.InnerWriter.ToString(); 

        html = REGEX_LINE_BREAKS.Replace(html, string.Empty); 

        html = REGEX_LINE_SPACE.Replace(html, string.Empty); 

        html = REGEX_SPACE.Replace(html, " "); 

        html = regReplaceBlank.Replace(regReplaceLine.Replace(html, ""), "><"); 

        writer.Write(html.Trim()); 

    } 

 

 

 

  摘自 jacky_163的专栏

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,