views:

5427

answers:

5

I would like to know if there is a simple way to parse HTML in vb.net. I know that HTML is not sctrict subset of XML, but it would be nice if it could be treated that way. Is there anything out there that would let me parse HTML in an XML-like way in VB.net?

+6  A: 

I like Html Agility pack - it's very developer friendly, free and source code is available.

TcKs
Hopeless documentation != developer friendly
FerretallicA
But selfdocumentation code developer friendly is. I understand that term "developer friendly" can be very subjective, however I tried several ways to parse/modify HTML code and this one is simple the best (for .NET and for fee-free), what you can get and absence of documentation don't change it. It's cruel reality.
TcKs
+1  A: 

Is it well formed? If the HTML is in fact well formed then it can be parsed as XML. If it is tag soup and there are unclosed elements and such I would think you would have to hunt around for a third-party solution.

Andrew Hare
What ever happened to accessing the document object model (dom) using mshtml?
Erx_VB.NExT.Coder
+3  A: 

If your HTML follows XHTML standards, you can do a lot of the parsing and processing using the System.XML namespace classes.

If, on the other hand, if what you're parsing is what web developers refer to as "tag soup," you'll need a third-party parser like HTML Agility Pack.

This may be only a partial solution to your problem if you're trying to figure out how a browser will interpret your HTML as each browser parses tag soup slightly differently.

Jekke
A: 

'add prog ref too: Microsoft.mshtml

'then on the page:

Imports mshtml

Function parseMyHtml(ByVal htmlToParse$) As String
    Dim htmlDocument As IHTMLDocument2 = New HTMLDocumentClass()
    htmlDocument.write(htmlToParse)
    htmlDocument.close()

    Dim allElements As IHTMLElementCollection = htmlDocument.body.all

    Dim allInputs As IHTMLElementCollection = allElements.tags("a")
    Dim element As IHTMLElement
    For Each element In allInputs
        element.title = element.innerText
    Next

    Return htmlDocument.body.innerHTML
End Function

'as found here: 'http://www.codeproject.com/KB/vb/Parsing_HTML.aspx

TripleHelix Tech
doesn't this essentially use the same libraries that IE uses to load its DOM? I've tried this before, but it always feels so dirty....
tooleb
A: 

Don't use agility pack, just use mshtml library to access the dom, this is what ie uses and is great for going through HTML elements.

Agility pack is nasty and unnecessarily hackie if you ask me, mshtml is the way to go. Look it up on msdn.

Erx_VB.NExT.Coder