.NET输出紧凑HTML

来源:未知 责任编辑:智问网络 发表时间:2013-10-07 00:59 点击:

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

 

如图:

\

 

 

方法一:

 

 

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.2cto.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的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1