您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C# 将 HTML 转成纯文本

2025/8/13 13:03:16发布20次查看
/// /// converts html to plain text. /// class htmltotext { // static data tables protected static dictionary _tags; protected static hashset _ignoretags; // instance variables protected textbuilder _text; protected string _html; protected int _pos; // static constructor (one time only) static htmltotext() { _tags = new dictionary(); _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(); _ignoretags.add(script); _ignoretags.add(noscript); _ignoretags.add(style); _ignoretags.add(object); } /// /// converts the given html to plain text and returns the result. /// /// html to be converted /// resulting plain text public string convert(string html) { // initialize state variables _text = new textbuilder(); _html = html; _pos = 0; // process input while (!endoftext) { if (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() == '= _html.length); } } // safely returns the character at the current position protected char peek() { return (_pos 0) flushcurrline(); return _text.tostring(); } } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product