惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - 在天空飞翔

asp net core 跨平台初体验 获取图片的主色调 U盘启动安装 window server 2003 简单的中文姓名生成器 扩展 DataGridView 的功能(五) 表白 天涯宝盒-天涯看贴脚本-只看楼主-自动翻页 使用 asp.net 编写的一些大中型的网站 PrecompiledApp.config 的惨剧 [音乐] the dream catcher csv 文件的读取 扩展DataGridView 的功能(四) 三八节快乐 将MP3文件嵌入到exe中并播放 [音乐] 下个路口见 雷人的面试 发现不明飞行物 扩展 DataGridView 的功能(三) 扩展 DataGridView 的功能(二)
WebBrowser - 想说爱你不容易
在天空飞翔 · 2012-06-28 · via 博客园 - 在天空飞翔

最近项目中需要在 winform 中加载 xhtml 文件并修改,颇废了一些周折,特发文纪念,同时讨论下有没有更好的办法。

在winform中加载网页首先想到的(其实目前也只能使用)当然是使用 WebBrowser 控件,但是。。。这个控件真不太好用。

1. xp 下不支持 xhtml。 使用 Navigate 方法加载本地的一个 xhtml 文件, 居然弹出下载窗口。

    (win7 下正常,但是现在xp不还是王道嘛)

   肿么办:将文件名改为 html 就行了, 处理完毕后再把文件改回 xhtml。

2. 使用 WebBrowser 打开后, 再通过 document.documentElement.outerHTML 取得的 html 代码中所有的 attribute 的引号全丢失了

    例如源文件为 <div class="top" id="my_edit" > 

    打开后就变成 <div class=top id=my_edit>

3. 同第2点,html 代码的标签全部变成大写

    例如源文件为 <div id="my_edit" > </div>
    打开后就变成 <DIV id="my_edit"> </DIV>

4. 同第2点,html 代码中的 img 标签自动变成非闭合

    例如源文件为 <img src="images/chjy.gif" ></img>

    打开后就变成 <IMG src="images/chjy.gif"> 

5. 同第2点,html 代码中的 BR 标签全部变成非闭合

    例如源文件为 <br/>

    打开后就变成 <BR> 

以上的 2,3,4,5 点在 xhtml 下都是属于不规范的代码,根本无法打开,没办法,只能一个个用正则修改。

附上代码:

//标签全部变小写
                html = Regex.Replace(html, @"(?<=\</?)\w+"delegate(Match m)
                {
                    return m.Value.ToLower();
                });
                //属性值加上双引号
                html = Regex.Replace(html, @"=([^""]+?)([\s>])"delegate(Match m)
                {
                    return "=\"" + m.Groups[1].Value + "\"" + m.Groups[2].Value;
                });
                //img闭合
                html = Regex.Replace(html, "(?<=\\<img[^>]+)>"delegate(Match m)
                {
                    return "/>";
                }, RegexOptions.IgnoreCase);
                //br 闭合

                html = Regex.Replace(html, "<BR>""<br/>", RegexOptions.IgnoreCase); 

修改后勉强可以用了,但是不知道有没有后遗症啊。

高手们说说,我这样的作法对吗?