65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 .NET Framework 进行 HTML 解析

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.26/5 (9投票s)

2007 年 6 月 29 日

2分钟阅读

viewsIcon

131946

downloadIcon

2828

在本文中,我将展示如何使用一个漂亮且有用的 .NET 程序集来解析和修改一段 HTML 代码。

下载此示例的演示类

引言

再次问候!

在本文中,我们将使用一个 .NET Framework 程序集来解析 HTML。
该命名空间称为 **mshtml**,我将展示这个程序集及其对象如何在基本方面如此有用。

程序集

该程序集位于以下文件夹:

Screenshot - mshtml.jpg

如果您正在处理远程网站/共享驱动器,只需将此程序集复制并粘贴到 /bin 文件夹中。

首先,我们需要将程序集引用添加到我们的项目中。

项目属性 >> 添加引用 >> .NET >> Microsoft.mshtml

HTML 块

这是我们将要处理的简单 HTML 块示例:

Dim myHTML$ = _
          vbCrLf & "<html>" _
        & vbCrLf & "<body>" _
        & vbCrLf & "<input type=""text"" name=""myTextBox""/>" _
        & vbCrLf & "<input type=""button"" name=""myButton""/>" _
        & vbCrLf & "<input type=""checkbox"" name=""myCheckBox"" class=""removeMe""/>" _
        & vbCrLf & "</body>" _
        & vbCrLf & "</html>"


正如您所见,它很简单,只有 3 个输入。

对象

在此程序集中,我们拥有代表我们 **HTML** 标签的所有对象。在此示例中,我将专注于 **INPUT** 标签,使用对象:**IHTMLInputElement**。

但我们有一个通用的对象,它为我们提供了额外的属性:**IHTMLElement**。
要访问这些对象,请不要忘记以下内容:

Imports mshtml

该函数

这里是我们将在使用的完整函数,带有一些注释:

    Function parseMyHtml(ByVal htmlToParse$)

        '::......... Declare a new HTML document to use, and write our normal HTML
        Dim htmlDocument As IHTMLDocument2 = New HTMLDocumentClass()
        htmlDocument.write(htmlToParse)
        htmlDocument.close()

        '::......... With this we retrieve all of the HTML elements collection existing on out HTML block
        Dim allElements As IHTMLElementCollection = htmlDocument.body.all

        '::......... Find by name out INPUT element on the group, and set a new value
        Dim myTextBox As IHTMLInputElement = allElements.item("myTextBox")
        myTextBox.value = "This is my text box!"

        '::......... Our button, but now its a "IHTMLElement", the generic object, that gives us more properties
        '::......... And set a new attribute to our element
        Dim myButton As IHTMLElement = allElements.item("myButton")
        myButton.setAttribute("onClick", "javascript:alert('This is the button!')")

        '::......... As a input, we set its value
        Dim myButton2 As IHTMLInputElement = allElements.item("myButton")
        myButton2.value = "Click me!"

        '::......... Get the INPUT group of elements
        Dim allInputs As IHTMLElementCollection = allElements.tags("input")
        Dim element As IHTMLElement

        '::......... Change some properties
        For Each element In allInputs
            element.style.border = "1px solid red"
            element.style.fontFamily = "Verdana"

            '::......... I dont want any "removeMe" classed elements, so lets remove them
            If element.className = "removeMe" Then
                element.outerHTML = ""
            End If
        Next

        '::......... Return the parent element content ( BODY > HTML )
        Return htmlDocument.body.parentElement.outerHTML

    End Function

在此函数中,我们可以看到一些对象及其函数/属性的用法。

结果 HTML

调用函数并将结果显示给用户后,我们得到了以下 **HTML** 处理块:

<HTML><HEAD></HEAD>
<BODY><INPUT style="BORDER-RIGHT: red 1px solid; BORDER-TOP: red 1px solid; BORDER-LEFT: red 1px solid;
 BORDER-BOTTOM: red 1px solid; FONT-FAMILY: Verdana" value="This is my text box!" name=myTextBox>
 <INPUT style="BORDER-RIGHT: red 1px solid; BORDER-TOP: red 1px solid; BORDER-LEFT: red 1px solid; 
BORDER-BOTTOM: red 1px solid; FONT-FAMILY: Verdana" type=button value="Click me!" 
name=myButton onClick="javascript:alert('This is the button!')">  </BODY></HTML>

代码有点粗糙,因为 **mshtml** 会对其进行处理,并将每个属性(如 *style:border*)设置为单独的边框,但请相信我,浏览器会识别它。

结论

在用另一个页面测试了这个程序集之后,一个很大的页面,几乎有 120KB 的纯 HTML,包含 Javascript/CSS、图像和其他一些东西,这个程序集表现得非常强大,并且没有报告任何错误。

当您拥有用户输入的一些 HTML 并需要更改某些属性时,它非常有用。

© . All rights reserved.