Converting HTML to a C# String


Today I was working on a project where it became useful to convert an HTML file into two C# strings.  As I only intended to do this one time, I opted to not do all of the string replacements necessary and decided to look for someone else who had already done the heavy lifting. 

In short time I found a singularly useful tool to do exactly what I needed.   The tool is named HTML To C# String and is published as an open source project under the GPL (or MIT) license. (it isn’t extremely clear which)

It's simple.

It's simple.

The only limitation I found was that this project does not preserve whitespace or line breaks. That’s not a problem if you’re dealing with simple html, but if you have anything fancy–like javascript, you might run into troubles. For example this:

var something = "This is such a cool var!"; //A comment about this var
var somethingElse = "blah";

becomes this:

var something = "This is such a cool var!"; //A comment about this var var somethingElse = "blah";

This will obviously not work.  I converted his project (which is only a few lines of code) into a Visual Studio 2008 project and tweaked it slightly.  You can download the result below.  I did not compile it with a setup as this utility is too simple to warrant an installation in my humble opinion.  Just run the executable in bin/Release. 

Download it now…
HTML To C# String-Mod

,

  1. #1 by Adam at March 24th, 2009

    Nice, thanks a lot!

(will not be published)
  1. No trackbacks yet.