简而言之,HAP并不适合完成您的任务。我的笔记如下:
Imports HtmlAgilityPack
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mystring As String = "<BR>Terms of Service<BR></BR>Developers<BR>"
Dim myDoc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
myDoc.LoadHtml(mystring)
' here we notice HAP immediately discards the junk tag </br>
MsgBox(myDoc.DocumentNode.OuterHtml)
' Below we notice that HAP did not close the BR tag because it only
' attempts to close
' certain nested tags associated with tables (th, tr, td) and lists
' (li).
' if this was a supported tag that HAP could fix, the fixed output
' would be as follows:
' <br>Terms of Service<br></br>Developers<br></br></br>
' this string would be parsed as if the last tag closes the first
' and each set of
' inner tags close themselves without any text between them.
' This means even if you changed BR to TD, or some other tag HAP
' fixes nesting on, it
' still would not help to parse this correctly.
' Also HAP does not appear to support XHTML in this .net 2.0 version.
myDoc.OptionFixNestedTags = True
MsgBox(myDoc.DocumentNode.OuterHtml)
' here we put the BR tag into a collection. as it iterates through
' the tags we notice there is no inner text on the BR tag, presumably
' because of two reasons.
' 1. HAP will not close a BR.
' 2. It does not fix your broken nested tags as you expect or required.
Dim myBR As HtmlNodeCollection = myDoc.DocumentNode.SelectNodes("//BR")
If Not myBR Is Nothing Then
For Each br In myBR
MsgBox(br.InnerText)
Next
End If
End Sub
End Class
尝试选择节点'“// br”',循环遍历结果节点,并将标签设置为'InnerText'。 –
我还是使用htmagilitypack的初学者,你能不能在代码中告诉我你在说什么? :) – Jeff
即时通讯不知道什么是''是。请问为什么你的换行符有结束标签?无论如何,我可能会得到答案。支持... – 2014-01-31 01:59:51