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

推荐订阅源

T
Threatpost
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
I
Intezer
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
T
Tor Project blog
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Schneier on Security
Schneier on Security
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
The Register - Security
The Register - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
L
LangChain Blog
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
博客园 - 叶小钗
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
V
Visual Studio Blog
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
宝玉的分享
宝玉的分享
Vercel News
Vercel News
D
DataBreaches.Net
P
Palo Alto Networks Blog
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 在天空飞翔

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); 

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

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