C#实现将HTML转换成纯文本的方法

本文实例讲述了C#实现将HTML转换成纯文本的方法。分享给大家供大家参考。具体如下:

使用方法:

代码如下:

HtmlToText convert = new HtmlToText();
textBox2.Text = convert.Convert(textBox1.Text);

C#代码如下:

/// <summary>
/// Converts HTML to plain text.
/// </summary>
class HtmlToText
{
  // Static data tables
  protected static Dictionary<string, string> _tags;
  protected static HashSet<string> _ignoreTags;
  // Instance variables
  protected TextBuilder _text;
  protected string _html;
  protected int _pos;
  // Static constructor (one time only)
  static HtmlToText()
  {
    _tags = new Dictionary<string, string>();
    _tags.Add("address", "\n");
    _tags.Add("blockquote", "\n");
    _tags.Add("div", "\n");
    _tags.Add("dl", "\n");
    _tags.Add("fieldset", "\n");
    _tags.Add("form", "\n");
    _tags.Add("h1", "\n");
    _tags.Add("/h1", "\n");
    _tags.Add("h2", "\n");
    _tags.Add("/h2", "\n");
    _tags.Add("h3", "\n");
    _tags.Add("/h3", "\n");
    _tags.Add("h4", "\n");
    _tags.Add("/h4", "\n");
    _tags.Add("h5", "\n");
    _tags.Add("/h5", "\n");
    _tags.Add("h6", "\n");
    _tags.Add("/h6", "\n");
    _tags.Add("p", "\n");
    _tags.Add("/p", "\n");
    _tags.Add("table", "\n");
    _tags.Add("/table", "\n");
    _tags.Add("ul", "\n");
    _tags.Add("/ul", "\n");
    _tags.Add("ol", "\n");
    _tags.Add("/ol", "\n");
    _tags.Add("/li", "\n");
    _tags.Add("br", "\n");
    _tags.Add("/td", "\t");
    _tags.Add("/tr", "\n");
    _tags.Add("/pre", "\n");
    _ignoreTags = new HashSet<string>();
    _ignoreTags.Add("script");
    _ignoreTags.Add("noscript");
    _ignoreTags.Add("style");
    _ignoreTags.Add("object");
  }
  /// <summary>
  /// Converts the given HTML to plain text and returns the result.
  /// </summary>
  /// <param name="html">HTML to be converted</param>
  /// <returns>Resulting plain text</returns>
  public string Convert(string html)
  {
    // Initialize state variables
    _text = new TextBuilder();
    _html = html;
    _pos = 0;
    // Process input
    while (!EndOfText)
    {
      if (Peek() == '<')
      {
        // HTML tag
        bool selfClosing;
        string tag = ParseTag(out selfClosing);
        // Handle special tag cases
        if (tag == "body")
        {
          // Discard content before <body>
          _text.Clear();
        }
        else if (tag == "/body")
        {
          // Discard content after </body>
          _pos = _html.Length;
        }
        else if (tag == "pre")
        {
          // Enter preformatted mode
          _text.Preformatted = true;
          EatWhitespaceToNextLine();
        }
        else if (tag == "/pre")
        {
          // Exit preformatted mode
          _text.Preformatted = false;
        }
        string value;
        if (_tags.TryGetValue(tag, out value))
          _text.Write(value);
        if (_ignoreTags.Contains(tag))
          EatInnerContent(tag);
      }
      else if (Char.IsWhiteSpace(Peek()))
      {
        // Whitespace (treat all as space)
        _text.Write(_text.Preformatted ? Peek() : ' ');
        MoveAhead();
      }
      else
      {
        // Other text
        _text.Write(Peek());
        MoveAhead();
      }
    }
    // Return result
    return HttpUtility.HtmlDecode(_text.ToString());
  }
  // Eats all characters that are part of the current tag
  // and returns information about that tag
  protected string ParseTag(out bool selfClosing)
  {
    string tag = String.Empty;
    selfClosing = false;
    if (Peek() == '<')
    {
      MoveAhead();
      // Parse tag name
      EatWhitespace();
      int start = _pos;
      if (Peek() == '/')
        MoveAhead();
      while (!EndOfText && !Char.IsWhiteSpace(Peek()) &&
        Peek() != '/' && Peek() != '>')
        MoveAhead();
      tag = _html.Substring(start, _pos - start).ToLower();
      // Parse rest of tag
      while (!EndOfText && Peek() != '>')
      {
        if (Peek() == '"' || Peek() == '\'')
          EatQuotedValue();
        else
        {
          if (Peek() == '/')
            selfClosing = true;
          MoveAhead();
        }
      }
      MoveAhead();
    }
    return tag;
  }
  // Consumes inner content from the current tag
  protected void EatInnerContent(string tag)
  {
    string endTag = "/" + tag;
    while (!EndOfText)
    {
      if (Peek() == '<')
      {
        // Consume a tag
        bool selfClosing;
        if (ParseTag(out selfClosing) == endTag)
          return;
        // Use recursion to consume nested tags
        if (!selfClosing && !tag.StartsWith("/"))
          EatInnerContent(tag);
      }
      else MoveAhead();
    }
  }
  // Returns true if the current position is at the end of
  // the string
  protected bool EndOfText
  {
    get { return (_pos >= _html.Length); }
  }
  // Safely returns the character at the current position
  protected char Peek()
  {
    return (_pos < _html.Length) ? _html[_pos] : (char)0;
  }
  // Safely advances to current position to the next character
  protected void MoveAhead()
  {
    _pos = Math.Min(_pos + 1, _html.Length);
  }
  // Moves the current position to the next non-whitespace
  // character.
  protected void EatWhitespace()
  {
    while (Char.IsWhiteSpace(Peek()))
      MoveAhead();
  }
  // Moves the current position to the next non-whitespace
  // character or the start of the next line, whichever
  // comes first
  protected void EatWhitespaceToNextLine()
  {
    while (Char.IsWhiteSpace(Peek()))
    {
      char c = Peek();
      MoveAhead();
      if (c == '\n')
        break;
    }
  }
  // Moves the current position past a quoted value
  protected void EatQuotedValue()
  {
    char c = Peek();
    if (c == '"' || c == '\'')
    {
      // Opening quote
      MoveAhead();
      // Find end of value
      int start = _pos;
      _pos = _html.IndexOfAny(new char[] { c, '\r', '\n' }, _pos);
      if (_pos < 0)
        _pos = _html.Length;
      else
        MoveAhead();  // Closing quote
    }
  }
  /// <summary>
  /// A StringBuilder class that helps eliminate excess whitespace.
  /// </summary>
  protected class TextBuilder
  {
    private StringBuilder _text;
    private StringBuilder _currLine;
    private int _emptyLines;
    private bool _preformatted;
    // Construction
    public TextBuilder()
    {
      _text = new StringBuilder();
      _currLine = new StringBuilder();
      _emptyLines = 0;
      _preformatted = false;
    }
    /// <summary>
    /// Normally, extra whitespace characters are discarded.
    /// If this property is set to true, they are passed
    /// through unchanged.
    /// </summary>
    public bool Preformatted
    {
      get
      {
        return _preformatted;
      }
      set
      {
        if (value)
        {
          // Clear line buffer if changing to
          // preformatted mode
          if (_currLine.Length > 0)
            FlushCurrLine();
          _emptyLines = 0;
        }
        _preformatted = value;
      }
    }
    /// <summary>
    /// Clears all current text.
    /// </summary>
    public void Clear()
    {
      _text.Length = 0;
      _currLine.Length = 0;
      _emptyLines = 0;
    }
    /// <summary>
    /// Writes the given string to the output buffer.
    /// </summary>
    /// <param name="s"></param>
    public void Write(string s)
    {
      foreach (char c in s)
        Write(c);
    }
    /// <summary>
    /// Writes the given character to the output buffer.
    /// </summary>
    /// <param name="c">Character to write</param>
    public void Write(char c)
    {
      if (_preformatted)
      {
        // Write preformatted character
        _text.Append(c);
      }
      else
      {
        if (c == '\r')
        {
          // Ignore carriage returns. We'll process
          // '\n' if it comes next
        }
        else if (c == '\n')
        {
          // Flush current line
          FlushCurrLine();
        }
        else if (Char.IsWhiteSpace(c))
        {
          // Write single space character
          int len = _currLine.Length;
          if (len == 0 || !Char.IsWhiteSpace(_currLine[len - 1]))
            _currLine.Append(' ');
        }
        else
        {
          // Add character to current line
          _currLine.Append(c);
        }
      }
    }
    // Appends the current line to output buffer
    protected void FlushCurrLine()
    {
      // Get current line
      string line = _currLine.ToString().Trim();
      // Determine if line contains non-space characters
      string tmp = line.Replace(" ", String.Empty);
      if (tmp.Length == 0)
      {
        // An empty line
        _emptyLines++;
        if (_emptyLines < 2 && _text.Length > 0)
          _text.AppendLine(line);
      }
      else
      {
        // A non-empty line
        _emptyLines = 0;
        _text.AppendLine(line);
      }
      // Reset current line
      _currLine.Length = 0;
    }
    /// <summary>
    /// Returns the current output as a string.
    /// </summary>
    public override string ToString()
    {
      if (_currLine.Length > 0)
        FlushCurrLine();
      return _text.ToString();
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C#正则表达式匹配HTML中的图片路径,图片地址代码

    一般来说一个 HTML 文档有很多标签,比如"<html>"."<body>"."<table>"等,想把文档中的 img 标签提取出来并不是一件容易的事.由于 img 标签样式变化多端,使提取的时候用程序寻找并不容易.于是想要寻找它们就必须写一个非常健全的正则表达式,不然有可能会找得不全,或者找出来的不是正确的 img 标签.我们可以从 HTML 标签的格式去想应该怎么建这个正则表达式.首先要想一下 img

  • C#获取HTML文本的第一张图片与截取内容摘要示例代码

    获取第一张图片 要我们获得到的数据是一段HTML文本.也许这段文本里面有许多图片.需要截取一张作为标题图片.也就是做为主图.这时就可以用到下面这个方法获取到第一张图片. 示例代码 #region 获取第一张图片 /// <summary> /// 获取HTML文本的图片地址 /// </summary> /// <param name="content"></param> /// <returns></returns&g

  • asp.net(C#) 动态添加非ASP的标准html控件(如添加Script标签)

    复制代码 代码如下: HtmlGenericControl Include2 = new HtmlGenericControl("script"); Include2.Attributes.Add("type", "text/javascript"); Include2.InnerHtml = "alert('JavaScript in Page Header');"; this.Page.Header.Controls.Ad

  • 使用C#获取网页HTML源码的例子

    最近在做一个项目,其中一个功能是根据一个URL地址,获取到网页的源代码.在ASP.NET(C#)中,获取网页源代码貌似有很多种方法,我随便搞了一个简单的WebClient,非常简单容易.但后面一个非常恼火的问题出来了,那就是中文的乱码. 通过仔细研究,中文的网页不外乎GB2312和UTF-8这两种编码.于是有了下面这段代码: 复制代码 代码如下: /// <summary>        /// 根据网址的URL,获取源代码HTML        /// </summary>   

  • c#中过滤html的正则表达式

    实现代码 /// <summary> /// 去除HTML标记 /// </summary> /// <param name="NoHTML">包括HTML的源码 </param> /// <returns>已经去除后的文字</returns> public static string NoHTML(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htm

  • ASP.net(c#) 生成html的几种解决方案[思路]第1/2页

    方案1:  复制代码 代码如下: /// <summary > /// 传入URL返回网页的html代码 /// </summary > /// <param name="Url" >URL </param > /// <returns > </returns > public static string getUrltoHtml(string Url) { errorMsg = ""; try

  • C#导出生成excel文件的方法小结(xml,html方式)

    直接贴上代码,里面都有注释 复制代码 代码如下: /// <summary> /// xml格式生成excel文件并存盘;        /// </summary>        /// <param name="page">生成报表的页面,没有传null</param>        /// <param name="dt">数据表</param>        /// <param

  • C#将html table 导出成excel实例

    复制代码 代码如下: public void ProcessRequest (HttpContext context) { string elxStr = "<table><tbody><tr><td>1</td><td>11</td></tr><tr><td>2</td><td>22</td></tr></tbody>

  • C#实现下载网页HTML源码的方法

    本文实例讲述了C#实现下载网页HTML源码的方法.分享给大家供大家参考之用.具体方法如下: public static class DownLoad_HTML { private static int FailCount = 0; //记录下载失败的次数 public static string GetHtml(string url) //传入要下载的网址 { string str = string.Empty; try { System.Net.WebRequest request = Sys

  • C#下解析HTML的两种方法介绍

    在搜索引擎的开发中,我们需要对Html进行解析.本文介绍C#解析HTML的两种方法.AD: 在搜索引擎的开发中,我们需要对网页的Html内容进行检索,难免的就需要对Html进行解析.拆分每一个节点并且获取节点间的内容.此文介绍两种C#解析Html的方法. C#解析Html的第一种方法:用System.Net.WebClient下载Web Page存到本地文件或者String中,用正则表达式来分析.这个方法可以用在Web Crawler等需要分析很多Web Page的应用中.估计这也是大家最直接,

随机推荐