先解码。接着转换为纯文本,用这段代码:
public static string HtmlToText(string source)
        {
            string result;

//remove line breaks,tabs
            result = source.Replace("\r", " ");
            result = result.Replace("\n", " ");
            result = result.Replace("\t", " ");

//remove the header
            result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase);

result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase);

//remove all styles
            result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase); //clearing attributes
            result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase);

//insert tabs in spaces of <td> tags
            result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase);

//insert line breaks in places of <br> and <li> tags
            result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase);

//insert line paragraphs in places of <tr> and <p> tags
            result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase);

//remove anything thats enclosed inside < >
            result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);

//replace special characters:
            result = Regex.Replace(result, @"&amp;", "&", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"&nbsp;", " ", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"&lt;", "<", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"&gt;", ">", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"&(.{2,6});", string.Empty, RegexOptions.IgnoreCase);

//remove extra line breaks and tabs
            result = Regex.Replace(result, @" ( )+", " ");
            result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
            result = Regex.Replace(result, @"(\r\r)+", "\r\n");

return result;
        }

转载于:https://www.cnblogs.com/walleyekneel/archive/2011/09/20/2182635.html

html代码转换成为纯文本相关推荐

  1. endnote转化成纯文本后_EndNote转换成BibTeX格式

    用Word写小文档,有天生的优势.可是对于长篇大论,既有章节又要编排目录的情况下,格式的问题会非常头疼.此时LaTeX的优势便逐渐显现出来(LaTeX和BibTeX都是免费软件).正如下图所示(图片来 ...

  2. 自动生成纯文本表格的工具

    https://tableconvert.com/?output=text 有时候需要写文档的时候生成这种纯文本表格,这个工具真的很方便,贴上数据就可以了.

  3. IOS使用正则表达式去掉html中的标签元素,获得纯文本

    IOS使用正则表达式去掉html中的标签元素,获得纯文本 content是根据网址获得的网页源码字符串 NSRegularExpression *regularExpretion=[NSRegular ...

  4. 一行代码:你的纯文本秒变Markdown

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来自:公众号 机器之心 只要你有纯文本编辑器,加上一条语句,瞬间它 ...

  5. 只需一行代码,纯文本秒变Markdown

    机器之心 机器之心编辑部 只要你有纯文本编辑器,加上一条语句,瞬间它就可以成为 Markdown 编辑器. Markdeep 是一个用来写纯文本的插件,它能以 Markdown 的语法与渲染方式纯文本 ...

  6. 只需一行代码,你的纯文本秒变 Markdown

    点击上方"视学算法",选择"星标"公众号 重磅干货,第一时间送达 只要你有纯文本编辑器,加上一条语句,瞬间它就可以成为 Markdown 编辑器. Markde ...

  7. endnote转化成纯文本后_如何用Endnote分分钟搞定参考文献

    原标题:如何用Endnote分分钟搞定参考文献 解螺旋公众号·陪伴你科研的第1924天 Endnote教程第二弹来啦! 写论文离不了对参考文献的引用,Endnote在文献撰写中发挥着重要作用.下面我们 ...

  8. 只需一行代码,你的纯文本秒变Markdown

    关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 本文转载自:机器之心 只要你有纯文本编辑器,加上一条语句,瞬间它就可以成为 Mar ...

  9. LFCS 系列第二讲:如何安装和使用纯文本编辑器 vi/vim

    LFCS 系列第二讲:如何安装和使用纯文本编辑器 vi/vim 几个月前, Linux 基金会发起了 LFCS (Linux 基金会认证系统管理员Linux Foundation Certified ...

最新文章

  1. oracle 分区表的建立方法
  2. python自学免费课堂-如何系统地自学 Python?
  3. C++ Primer 5th笔记(chap 19 特殊工具与技术)成员函数指针
  4. 2.2.4 调度算法: 先来先服务 最短作业优先 最高相应比优先
  5. 【Java基本功】一文读懂String及其包装类的实现原理
  6. Android ListView headerDividers 分割线显示隐藏问题
  7. 《The Last Night》美术分析
  8. MyEclipse for Windows快捷键
  9. 解决 error: Your local changes to the following files would be overwritten by merge:XXXX
  10. MATLAB编辑GUI界面
  11. html5 漂亮的左右布局_2020年庚子年风水布局,2020年家居风水布局汇总 | 影楼
  12. java 使用apollo,Springboot apollo原理及使用方法详解
  13. java怎么从大到小排序元祖_列表、元祖的操作
  14. nullnullUVa 10066 - The Twin Towers(LCS水题)
  15. 由一个照片,可以看出云是个物体
  16. 键 -- 主键、候选键、可选键
  17. Data too long for column ‘password‘ at row 1“
  18. 通过EXCEL中的FILTERXML函数实现批量翻译
  19. Springcould学习总结
  20. 吸血鬼数字java_吸血鬼数字

热门文章

  1. PHP框架之间有什么区别,php框架和设计模式区别
  2. android 选项卡TabHost
  3. andpods授权码订单号分享_不要再让你的接口裸奔了,Boot快速尝试OAuth2密码和授权码模式...
  4. 功能测试Bug频发?测试工作中这点不可忽略
  5. oracle取32位唯一码,Oracle导出导入dmp文件命令,以及excel生成32位uuid
  6. mysql没有err文件_xampp中的mysql启动时无法产生err文件
  7. 怎么写遮罩层 css,css案例 - mask遮罩层的华丽写法
  8. python 调用api上传物流信息,python实现快递鸟API物流查询接口 数据签名方法
  9. android ijkplayer使用_Ijkplayer、ExoPlayer、VLC播放器综合比较
  10. C语言不用strcmp函数比较字符串大小