Validate Web Page Against W3C Validator in .NET

During one of the redesigns of DeepFitness.com, I decided to make sure all the articles validated to XHTML.  The post Converting HTML to XHTML Programmatically in .NET shows how I did that.  FreeTextBox did an excellent job assisting with the conversion, but it wasn’t perfect.  A small percentage of the articles were not XHTML and I needed a way to know which ones needed to be fixed by hand.  Hand entering over 1,000 article URLs into the W3C validator was not an option.

Below is some code that sends a URL to be tested against the W3C Validator. It returns a true or false.  If you have a lot of URLs to test – like I did – just add some reporting code to flag the ones that don’t pass.

using System.Net;

public bool IsPageValid(string url)
{
    string validatorURL = "http://validator.w3.org/check?uri=";
    string checkURL = validatorURL + url;
    string validStatus = "Invalid";

    // create the request
    HttpWebRequest request = WebRequest.Create(checkURL) as HttpWebRequest;
    // instruct the server to return headers only
    request.Method = "HEAD";
    // make the connection
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    WebHeaderCollection headers = response.Headers;

    foreach (string key in headers.Keys)
    {
        if (key == "X-W3C-Validator-Status")
            validStatus = headers[key];
    }

    if (validStatus == "Valid")
    {
        return true;
    }
    else
    {
        return false;
    }
}

Lab Validate DOCTYPE in ASP.NET

This entry was posted in ASP.NET, C# and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>