将htmlagilitypack节点转换为htmlelement .net

时间:2012-02-24 22:53:13

标签: .net html-agility-pack

我刚刚使用htmlagilitypack从html文档中提取所有链接为htmlnode,但我需要从我的函数返回htmlelement

Dim Tags As HtmlNodeCollection = docNode.SelectNodes(strXpath)
  Dim ListResult As New List(Of HtmlElement)
  For Each Tag As HtmlNode In Tags
     ListResult.Add(Tag.Element)
 Next
Return Nothing

我该怎么做?

1 个答案:

答案 0 :(得分:2)

我怀疑唯一的方法是创建HtmlElement,然后从HtmlNode复制属性和内部HTML。

这是一个扩展方法;它接受对System.Windows.Forms.HtmlDocument实例的引用,以创建新的HtmlElement

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ToHtmlElement(node As HtmlNode, doc As System.Windows.Forms.HtmlDocument) As HtmlElement
    Dim element = doc.CreateElement(node.Name)
    For Each attribute As HtmlAttribute In node.Attributes
        element.SetAttribute(attribute.Name, attribute.Value)
    Next
    element.InnerHtml = node.InnerHtml

    Return element
End Function

使用它你可以这样:

Dim browser As New WebBrowser()
browser.Navigate("about::blank")

Dim Tags As HtmlNodeCollection = docNode.SelectNodes(strXpath)
  Dim ListResult As New List(Of HtmlElement)
  For Each Tag As HtmlNode In Tags
     ListResult.Add(Tag.ToHtmlElement(browser.Document))
 Next
Return Nothing

但是请注意,我不太熟悉VB.NET,我首先为C#创建了代码示例,然后将它们翻译成VB,.NET。