提取所有表单元素名称htmlagilitypack

时间:2011-06-24 08:50:12

标签: vb.net winforms forms html-agility-pack

我有这段代码来提取html文档中的所有表单输入元素。目前,我无法获得select,textarea和除输入元素之外的其他元素。

Dim htmldoc As HtmlDocument = New HtmlDocument()
        htmldoc.LoadHtml(txtHtml.Text)
        Dim root As HtmlNode = htmldoc.DocumentNode
        If root Is Nothing Then
            tsslStatus.Text = "Error parsing html"
        End If
        ' parse the page content
        For Each InputTag As HtmlNode In root.SelectNodes("//input")
            'get title
            Dim attName As String = Nothing
            Dim attType As String = Nothing
            For Each att As HtmlAttribute In InputTag.Attributes
                Select Case att.Name.ToLower
                    Case "name"
                        attName = att.Value
                    Case "type"
                        attType = att.Value
                End Select
                If attName Is Nothing OrElse attType Is Nothing Then
                    Continue For
                End If
                Dim sResult As String = String.Format("Type={0},Name={1}", attType, attName).ToLower

                If txtResult.Text.Contains(sResult) = False Then
                    'Debug.Print(sResult)
                    txtResult.Text &= sResult & vbCrLf
                End If

            Next
        Next

有人可以帮我解决如何在html文档中获取所有表单中的所有元素吗?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,我所做的就是使用这个

Dim Tags As HtmlNodeCollection = docNode.SelectNodes("//input | //select | //textarea")

感谢您寻找