winfrom和正则表达式高手进来看下,就差一点小问题了!!!

skloveww 2009-12-16 11:11:32
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();未将对象引用设置到对象的实例,实在解决不明白了!!有高手帮答下吗!
...全文
142 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
龟仙 2009-12-16
  • 打赏
  • 举报
回复
skloveww 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zgke 的回复:]
.....WINFOR下使用 引用System.Web.dll

 

Htmlstring = System.Web.HttpUtility.HtmlEncode(Htmlstring).Trim();
[/Quote]

我引用没有System.Web.dll
zgke 2009-12-16
  • 打赏
  • 举报
回复
.....WINFOR下使用 引用System.Web.dll



Htmlstring = System.Web.HttpUtility.HtmlEncode(Htmlstring).Trim();
注册失败 2009-12-16
  • 打赏
  • 举报
回复
Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

你是winform的程序,怎么可能会有 HttpComtext呢?
你的 HttpContext.Current就是个NULL

要使用HtmlEncode,可以用 System.Web.HttpServerUtility.HtmlEncode
NewUser2008 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kingdomgps 的回复:]
Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

你是winform的程序,怎么可能会有 HttpComtext呢?
你的 HttpContext.Current就是个NULL

要使用HtmlEncode,可以用 System.Web.HttpServerUtility.HtmlEncode

[/Quote]

winform 没有httpContext
-过客- 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 skloveww 的回复:]
我引用没有System.Web.dll
[/Quote]

解决方案下的“引用”-> 右键 -> 添加引用 -> System.Web
wangxiao2008 2009-12-16
  • 打赏
  • 举报
回复
up

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧