黑简单的问题,c#如何操作HTML?

DavidNoWay 2008-10-15 03:20:52
想把一个HTML文件另存为TXT格式的。
请问如何操作。
注意,不是直接修改文件后缀,是要“另存为”txt格式。
比如百度,另存为显示
百度一下,你就知道登录




新 闻网 页贴 吧知 道MP3图 片视 频
帮助
高级
空间 hao123 | 更多>>
把百度设为主页企业推广 | 搜索风云榜 | 关于百度 | About Baidu©2008 Baidu 使用百度前必读 京ICP证030173号
...全文
437 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
DavidNoWay 2008-10-15
  • 打赏
  • 举报
回复

string filename = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;
dlg.Filter = "html文件 (*.html)|*.html";
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
filename = dlg.FileName;
}

StreamReader fileStream = new StreamReader(filename, Encoding.Default);
string content = fileStream.ReadToEnd();
content = Regex.Replace(content,@"<style>[\s\S]*</style>", string.Empty);
content = Regex.Replace(content, @"<script[\s\S]*>[\s\S]*</script>", string.Empty);
content = Regex.Replace(content, @"<(?:.|\s)*?>", "");
content = Regex.Replace(content, @"\&[^\;]*\;", " ");

StreamWriter sw = File.CreateText("D:\\data.txt");
sw.Write(content);
sw.Close();

原来要导入using System.Text.RegularExpressions;啊
搞定了,谢谢各位。
我姓区不姓区 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ayouaja 的回复:]
谢谢五楼的兄弟,其实html文件是在本地的机子上单个文件。不用考虑网络的问题哈。
再麻烦各位一下了。

[/Quote]
都写到这份上了,自己就不能琢磨琢磨

string content = System.IO.File.ReadAllText("D:\\index.html", Encoding.GetEncoding("gb2312"));
Console.WriteLine(content);
content = Regex.Replace(content, @"<style>[\s\S]*</style>", string.Empty);
content = Regex.Replace(content, @"<script[\s\S]*>[\s\S]*</script>", string.Empty);
content = Regex.Replace(content, @"<(?:.|\s)*?>", string.Empty);
content = Regex.Replace(content, @"\&[^\;]*\;", " ");
StreamWriter sw = File.CreateText("D:\\data.txt");
sw.Write(content);
sw.Close();

DavidNoWay 2008-10-15
  • 打赏
  • 举报
回复
唉,神啊救救我吧
DavidNoWay 2008-10-15
  • 打赏
  • 举报
回复

string filename = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;
dlg.Filter = "html文件 (*.html)|*.html";
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
filename = dlg.FileName;
}
StreamReader fileStream = new StreamReader(filename, Encoding.Default);
string content = fileStream.ReadToEnd();
content = Regex.Replace(@"<style>[\s\S]*</style>", string.Empty);
content = Regex.Replace(@"<script[\s\S]*>[\s\S]*</script>", string.Empty);
content = Regex.Replace(@"<(?:.|\s)*?>", string.Empty);
content = Regex.Replace(@"\&[^\;]*\;", " ");

StreamWriter sw = File.CreateText("D:\\data.txt");
sw.Write(content);
sw.Close();

提示“当前上下文中不存在名称“Regex””,Regex是什么东东呢?
jimmyzhang 2008-10-15
  • 打赏
  • 举报
回复
注意引用
using System.IO;
jimmyzhang 2008-10-15
  • 打赏
  • 举报
回复
string content = File.ReadAllText(Server.MapPath(".")+"\\your.htm").ToString();
content = Regex.Replace(content, @"<style>[\s\S]*</style>", string.Empty);
content = Regex.Replace(content, @"<script[\s\S]*>[\s\S]*</script>", string.Empty);
content = Regex.Replace(content, @"<(?:.|\s)*?>", string.Empty);
content = Regex.Replace(content, @"\&[^\;]*\;", " ");

System.IO.StreamWriter sw = File.CreateText(Server.MapPath(".")+"\\data.txt");
sw.Write(content);
sw.Close();
veiny 2008-10-15
  • 打赏
  • 举报
回复
正则表达过滤,得到正文

将<...>替换成null即String.Empty

content = Regex.Replace(content, @"<(?:.|\s)*?>", string.Empty);
Avoid 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ayouaja 的回复:]
谢谢五楼的兄弟,其实html文件是在本地的机子上单个文件。不用考虑网络的问题哈。
再麻烦各位一下了。
[/Quote]

你还是直接让人给你写个程序吧
DavidNoWay 2008-10-15
  • 打赏
  • 举报
回复
谢谢五楼的兄弟,其实html文件是在本地的机子上单个文件。不用考虑网络的问题哈。
再麻烦各位一下了。
怫悰 2008-10-15
  • 打赏
  • 举报
回复
冒泡

简单的话用WebBrowser的ShowSaveAsDialog
我姓区不姓区 2008-10-15
  • 打赏
  • 举报
回复

WebClient wc = new WebClient();
string content = wc.DownloadString("http://www.baidu.com");
Console.WriteLine(content);
content = Regex.Replace(content, @"<style>[\s\S]*</style>", string.Empty);
content = Regex.Replace(content, @"<script[\s\S]*>[\s\S]*</script>", string.Empty);
content = Regex.Replace(content, @"<(?:.|\s)*?>", string.Empty);
content = Regex.Replace(content, @"\&[^\;]*\;", " ");
StreamWriter sw = File.CreateText("D:\\data.txt");
sw.Write(content);
sw.Close();
/*
data.txt中的内容:
百度一下,你就知道
登录新 闻网 页贴 吧知 道MP3图 片视 频
帮助高级
空间 hao123 | 更多>>

把百度设为主页企业推广 | 搜索风云榜 | 关于百度 | About Baidu 2008 Baidu 使用百度前必读 京ICP证030173号
*/
cwblaze 2008-10-15
  • 打赏
  • 举报
回复
黑简单的问题,c#如何操作HTML?



黑简单的问题是什么意思!@#$%^&*(.....
DavidNoWay 2008-10-15
  • 打赏
  • 举报
回复
大侠webclient 是什么东东呢?另外,是用WINFORM的,能举个简单的例子么?
我姓区不姓区 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 juedaihuaihuai 的回复:]
webclient 读到内存里。然后正则过滤标签,然后输出到txt就ok了。
[/Quote]
正解
绝代坏坏 2008-10-15
  • 打赏
  • 举报
回复
webclient 读到内存里。然后正则过滤标签,然后输出到txt就ok了。

110,536

社区成员

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

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

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