htmlagilitypack node -> htmlelement.net conversion

.net html-agility-pack

Question

I recently utilized htmlagilitypack to extract every link from an html page as an htmlnode, but I need this returned from my function as an 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

How can I go about this?

1
0
2/27/2013 8:38:40 PM

Accepted Answer

I think the only way to achieve it is to develop new things.HtmlElement then copy the inner HTML and attributes fromHtmlNode .

The following extension method receives a reference to aSystem.Windows.Forms.HtmlDocument for example, to start a newHtmlElement :

<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

And here is how you may use it:

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

Please take notice that I'm not very familiar with VB.NET, therefore I initially wrote code examples in C# before translating them to VB.NET.

2
3/3/2012 9:58:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow