亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#實現將HTML轉換成純文本的方法

2020-01-24 01:36:50
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久99久久99精品免观看粉嫩| 午夜精品久久久久久久99热| 亚洲欧美国产精品va在线观看| 国产精品嫩草影院一区二区| 日韩免费视频在线观看| 91精品国产高清| 亚洲美女动态图120秒| 亚洲第一偷拍网| 亚洲第一免费网站| 亚洲成人网在线| 国产精品成熟老女人| 4k岛国日韩精品**专区| 久久亚洲精品小早川怜子66| 亚洲综合中文字幕在线| 欧美疯狂做受xxxx高潮| www.久久久久久.com| 俺去亚洲欧洲欧美日韩| 爱福利视频一区| 亚洲男人天堂视频| 麻豆精品精华液| 中文字幕亚洲自拍| 国产日韩综合一区二区性色av| 亚洲国产欧美日韩精品| 2019最新中文字幕| 亚洲视频在线观看网站| 欧美丝袜一区二区三区| 中文字幕亚洲欧美一区二区三区| 国产精品久久久久久久久久ktv| 国内偷自视频区视频综合| 38少妇精品导航| 欧美天天综合色影久久精品| 97在线免费视频| 欧美一级在线播放| 91精品久久久久久综合乱菊| 日韩麻豆第一页| 欧美乱大交做爰xxxⅹ性3| 亚洲色图综合网| 亚洲黄色av网站| 国产精品伦子伦免费视频| 亚洲三级免费看| 91国产精品电影| 久久露脸国产精品| 欧美激情一区二区久久久| 自拍亚洲一区欧美另类| 久久精品国产久精国产思思| 在线播放日韩av| 亚洲大胆人体视频| 欧美视频在线观看免费网址| 色综合久久久888| 亚洲激情视频网站| 久热精品视频在线观看一区| 国产成人自拍视频在线观看| 中文国产成人精品久久一| 欧美成人激情视频| 在线亚洲午夜片av大片| 欧美成人h版在线观看| 中文.日本.精品| 国产精品成熟老女人| 黑人精品xxx一区一二区| 亚洲精品乱码久久久久久按摩观| 午夜精品一区二区三区在线播放| 国产精品久久久久久久久久小说| 91色琪琪电影亚洲精品久久| 欧美精品在线极品| 欧美午夜影院在线视频| 亚洲人成电影在线播放| 亚洲a∨日韩av高清在线观看| 亚洲男人天堂古典| 午夜精品一区二区三区视频免费看| 51久久精品夜色国产麻豆| 久久久久久尹人网香蕉| 91av在线不卡| 亚洲国产成人91精品| 97超碰蝌蚪网人人做人人爽| 久久国产精品网站| 精品无人区太爽高潮在线播放| 国产精品视频久久| 中文字幕亚洲欧美一区二区三区| 国产精品自拍偷拍视频| 日韩小视频在线观看| 国产午夜精品免费一区二区三区| 久久久av网站| 亚洲自拍偷拍福利| 亚洲一二在线观看| 91av网站在线播放| 国产在线视频2019最新视频| 最近中文字幕2019免费| 欧美成人精品在线播放| 国产午夜精品免费一区二区三区| 97精品国产aⅴ7777| 国产精品视频999| 亚洲人成在线播放| 欧洲日本亚洲国产区| 日韩美女在线观看| 成人性生交大片免费看小说| 精品美女国产在线| 国产一区二区在线免费| 久久99久国产精品黄毛片入口| 国产视频在线一区二区| 国产成人综合精品在线| 欧美一级bbbbb性bbbb喷潮片| 亚洲视频欧美视频| 日本国产欧美一区二区三区| 国产精品免费看久久久香蕉| 亚洲精品久久久久久久久| 国产欧美日韩91| 91亚洲精品一区二区| 欧美精品免费在线观看| 亚洲日本欧美日韩高观看| 久久精品国产免费观看| 91久久久久久久久久久久久| 国产精品高清免费在线观看| 欧美一级淫片丝袜脚交| 久久噜噜噜精品国产亚洲综合| 日韩欧美综合在线视频| 91av在线影院| 亚洲美女激情视频| 国产精品视频中文字幕91| 亚洲国产成人精品一区二区| 久久69精品久久久久久国产越南| 久热精品在线视频| 久久久久久久久久久免费| 欧美日韩亚洲一区二区三区| 久久久久www| 亚洲天堂视频在线观看| 欧美性xxxxx极品| 国产在线视频91| 久久久综合免费视频| 国产成人精品免高潮在线观看| 欧美亚洲日本网站| 国产精品永久免费观看| 久99久在线视频| 日韩av综合网站| 日韩成人av网| 国产精品wwww| 日本久久精品视频| xvideos亚洲人网站| 亚洲欧美日韩直播| 国产z一区二区三区| 精品夜色国产国偷在线| 日本乱人伦a精品| 久久伊人精品天天| 亚洲一区二区三| 国产精品视频xxxx| 成人做爰www免费看视频网站| 中日韩午夜理伦电影免费| 亚洲一区www| 日本午夜在线亚洲.国产| 欧美资源在线观看| 青青久久aⅴ北条麻妃| 中文字幕日韩电影| 中文字幕亚洲字幕| 久久久免费高清电视剧观看| 亚洲福利视频免费观看| 亚洲精品videossex少妇| 成人精品网站在线观看| 国产97在线|亚洲| 亚洲风情亚aⅴ在线发布| 日韩精品久久久久久福利| 一区国产精品视频| 欧美精品日韩三级| 久久影院资源网| 亚洲日本成人网| 精品视频在线播放|