Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / Visual Basic
Article

MIL HTML Parser

Rate me:
Please Sign up or sign in to vote.
4.79/5 (77 votes)
30 Mar 20041 min read 393.8K   7.8K   154   74
A non-well-formed HTML parser for .NET

Introduction

This library produces a domain tree of a given HTML document, allowing the developer to navigate and change the document in an methodical way. In addition to the basic HTML production, this library can also be used to produce XHTML documents, as it includes an HTML 4 entity encoder. Included in this release is a demonstration application in VB.NET showing how to use the library. I hope that it is all fairly self-explanatory.

Background

This library was written to avoid having to convert a document into XML prior to reading, whilst preserving the distinct HTML qualities. This gets round some deployment issues I had with different platforms.

Using the code

The simplest way to use the code is to add it into your solution as a C# class library. There are no third-party dependencies so it is just a matter of adding the source files in. Alternatively, you can build the DLL and add it as a reference.

Points of Interest

The XHTML production is fairly basic - there is no built-in DTD checking. So far, I have had no problems in the generation, but I'm keen on getting that sorted.

History

  • 1.4
  • 1.3
    • Bugfix: <!DOCTYPE...> and <!...> now treated as comments
    • Bugfix: Malformed or incomplete attribute values causing infinite loop fixed
  • 1.2
    • Bugfix: <tag/> now handled properly
    • Bugfix: Parse errors of scripts
    • Bugfix: Parse errors of styles
    • HTML 4 entity encoding
    • DOM tree navigation
    • Basic node searching
    • HTML production
    • XHTML production (as per http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)
    • Added some component model stuff & comments
    • Hid the parser
  • 1.1
    • Initial release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Sweet Work Pin
Martin Fuchs27-Mar-04 7:49
Martin Fuchs27-Mar-04 7:49 
GeneralRe: Sweet Work Pin
Jacob Slusser27-Mar-04 11:38
Jacob Slusser27-Mar-04 11:38 
GeneralSgmlReader Pin
Jonathan de Halleux22-Mar-04 3:11
Jonathan de Halleux22-Mar-04 3:11 
GeneralSuggestions Pin
Stephane Rodriguez.22-Mar-04 2:53
Stephane Rodriguez.22-Mar-04 2:53 
GeneralRe: Suggestions Pin
GriffonRL22-Mar-04 10:06
GriffonRL22-Mar-04 10:06 
GeneralRe: Suggestions Pin
Stephane Rodriguez.22-Mar-04 18:59
Stephane Rodriguez.22-Mar-04 18:59 
GeneralRe: Suggestions Pin
GriffonRL22-Mar-04 20:46
GriffonRL22-Mar-04 20:46 
GeneralRe: Suggestions Pin
Stephane Rodriguez.22-Mar-04 22:13
Stephane Rodriguez.22-Mar-04 22:13 
GriffonRL wrote:
I don't know about URLmon.dll. I use it currently in a C# project but honestly I'm looking now for a pure C# library. I am more and more for 100% managed code. I'm fed up of interops and so on...

T'as pas dû chercher bien longtemps Wink | ;-)

HttpWebRequest h = (HttpWebRequest) 
WebRequest.Create("http://weblogs.asp.net/heatherleigh/contact.aspx");
StreamReader sr = new StreamReader( h.GetResponse().GetResponseStream() );
MessageBox.Show( sr.ReadToEnd() );


Et le tour est joué!




GriffonRL wrote:
You should be able to produce a nice library or control.

En fait, je l'ai développée. Une capture d'écran de LongSleeves ici.



GriffonRL wrote:
By the way, why noboby came up with a wrapper around Mozilla or tried to port some part of Mozilla to .NET ?

Basculement en anglais car ça peut toujours être utile aux autres. Mozilla has a wrapper Ax (mozctlx.dll) that not only mimics the IE renderer, but also exposes the same API, with appropriate events, and all that stuff. It's not that it doesn't exist, it's just that, in sites like codeproject.com, you get less links from non MS-components than otherwise. I wonder why btw when you know how much IE is full of bugs of many different natures and severities.

GriffonRL wrote:
I should probably browse the source again. This could be a good inspiration to right a HTML parser and other things.

Don't watch too much the source code in this article though, I think it's not worth it until a major rewrite. To me, a real strong html parser is one that can read html as well as xml, without changing a single line of code, and that provides at the same time a DOM model (read everything, store everything in memory) as well as an event-driven model (only the latest elements and attributes are known). May be SgmlReader (linked by Jonathan above) should be a given a look. At least SgmlReader is written by MS Chris Lovett, one of the fathers of msxml.
GeneralRe: Suggestions Pin
GriffonRL22-Mar-04 22:38
GriffonRL22-Mar-04 22:38 
GeneralRe: Suggestions Pin
Stephane Rodriguez.22-Mar-04 23:10
Stephane Rodriguez.22-Mar-04 23:10 
GeneralRe: Suggestions Pin
Anonymous22-Mar-04 20:49
Anonymous22-Mar-04 20:49 
GeneralAlready exists Pin
Rui Dias Lopes22-Mar-04 0:06
Rui Dias Lopes22-Mar-04 0:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.