Using the W3C HTML Validator API

Nov 22, 2006

The W3C has introduced an API for their HTML Validator. It is not a SOAP web service, but it does return XML that can be parsed, so now you have the ability to incorporate validation to any web application, Windows program or wherever you want. It works just as the normal validator, by specifying a URL, but you have to add the “output=soap12” parameter to the URL like so: http://validator.w3.org/check?uri=http://www.google.com&output=soap12.

I’ve been playing a little with the possibilities and created a class that encapsulates the validation process and wraps the results up in a suitable class. The collection of errors and warnings can be bound directly to a GridView or Repeater without additional code. This gives you a very easy, but powerful way of dealing with the validator.

Examples of use

Even thought the class is very small and its functionality limited, it still gives you all the relevant information from the validation. Here’s an example of some of the possibilities of the class.

using (HtmlValidator val = new HtmlValidator("http://www.google.com"))

{

  if (!val.Validate())

  {

    grid.DataSource = val.Errors; // or val.Warnings

    grid.DataBind();

    lbDoctype.Text = val.Doctype;

    lbCharset.Text = val.Charset.BodyName;

 

    foreach (HtmlValidator.ValidationError error in val.Errors)

    {

      DoSomething(error.Message, error.LineNumber, error.ColumnIndex);

    }

  }
}

Implementation

Download HtmlValidator.cs below and add it to any C# project. If you want to use it in an ASP.NET project, just put it in the App_Code folder.

Download

HtmlValidator.zip (1,43 KB)

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (4) -

NinjaCross
NinjaCross
11/22/2006 8:16:15 PM #

LOL.... it seems that we are running on the same road Laughing
Yesterday I was reading for W3C API specifications with the idea of building an automatic validator for my web site... and now you come with a complete solution ready to use... astonishing Laughing
Your solution is perfectly implemented, even if I usually like to implement tools like this one as stateless classes.
Probably I'll create a version of this class in a way that allows me to pass the URL to the Validate method, that in turns would return a descriptor containing the validation information (errors, warnings, etc).
In this manner the class would be allocated only once in multiple pages check loops.

Mads Kristensen
Mads Kristensen
11/23/2006 6:36:28 AM #

You can just add a static method to the class that returns a new instance of the class. The method should take a URL parameter. The reason I didn't make the entire class static was, that if you want to validate multiple URLs in a loop, you will get better performance by using the same instance and just change the URL property for each site.

A. de Vries
A. de Vries Netherlands
10/18/2007 7:55:59 PM #

Simple solution, exactly where i was looking for !

Andity
Andity
8/26/2008 8:54:10 AM #

Thanks!
How can I validate local html-file with this class?

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.