C#webbrowser改变了源代码

时间:2010-10-08 17:15:36

标签: c# webbrowser-control

我有一个webbrowser控件,我导航到包含此html的URL:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title></title>
</head>
<body marginheight="60" topmargin="60">
    <p align="center"><img src="nocontent.jpg" alt="" height="434" width="525" border="0" /></p>
</body>
</html>

但是当我使用这段代码来获取源代码时:

HTMLDocument objHtmlDoc = (HTMLDocument)browser.Document.DomDocument;
string pageSource = objHtmlDoc.documentElement.innerHTML;
Console.WriteLine(pageSource);

结果如下:

<HEAD><TITLE></TITLE>
<META content=text/html;charset=utf-8 http-equiv=content-type></HEAD>
<BODY topMargin=60 marginheight="60">
<P align=center><IMG border=0 alt="" src="nocontent.jpg" width=525 height=434></P></BODY>

这对进一步处理没有好处,我怎样才能确保它显示与我右键单击并选择“查看源”时相同的来源?

1 个答案:

答案 0 :(得分:6)

使用browser.DocumentText获取源HTML。

使用HTMLDocument类将使其从文档的概念模型生成HTML,而不是显示原始源。