using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text!=""){
FileStream fileOpen = new FileStream(textBox1.Text,FileMode.Open);
webBrowser1.DocumentStream = fileOpen;
StreamReader sr = new StreamReader(fileOpen);
string strHtml = sr.ReadToEnd();
strHtml = NoHtml.Ubb(textBox1.Text);
webBrowser1.DocumentText = strHtml;
}
} private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.RestoreDirectory = true;
open.FilterIndex = 1;
open.Filter = "文本文件(*.txt)|*.*|html文件(*html)|*.html|所有文件|*.*";
open.InitialDirectory = "c:\\";
if(open.ShowDialog()==DialogResult.OK){
textBox1.Text = open.FileName;
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}上面这是我from中的!!!using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;
using System.IO;
namespace WindowsApplication3
{
public class NoHtml
{
public static string Ubb(string Htmlstring)
{ Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring; } }
}
这个是单独创建个类写的正则表达式!
现在不懂的事,我现在运行就出现:Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();未将对象引用设置到对象的实例,实在解决不明白了!!有高手帮答下吗!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text!=""){
FileStream fileOpen = new FileStream(textBox1.Text,FileMode.Open);
webBrowser1.DocumentStream = fileOpen;
StreamReader sr = new StreamReader(fileOpen);
string strHtml = sr.ReadToEnd();
strHtml = NoHtml.Ubb(textBox1.Text);
webBrowser1.DocumentText = strHtml;
}
} private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.RestoreDirectory = true;
open.FilterIndex = 1;
open.Filter = "文本文件(*.txt)|*.*|html文件(*html)|*.html|所有文件|*.*";
open.InitialDirectory = "c:\\";
if(open.ShowDialog()==DialogResult.OK){
textBox1.Text = open.FileName;
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}上面这是我from中的!!!using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;
using System.IO;
namespace WindowsApplication3
{
public class NoHtml
{
public static string Ubb(string Htmlstring)
{ Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring; } }
}
这个是单独创建个类写的正则表达式!
现在不懂的事,我现在运行就出现:Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();未将对象引用设置到对象的实例,实在解决不明白了!!有高手帮答下吗!
你的 HttpContext.Current就是个NULL要使用HtmlEncode,可以用 System.Web.HttpServerUtility.HtmlEncode
解决方案下的“引用”-> 右键 -> 添加引用 -> System.Web