Ad Code

Converting HTML to XHTML - ASP.Net

Sometimes we are in a situation that we want to convert HTML to XHTML format. You can easily achieve this using the HTML2XHTML.dll library created by Microsoft.

You can download the library at: Click here to download

Next you need to register the library. Just go to the Visual Studio.NET command prompt and go to the folder which contains the HTML2XHTMLLib.dll file and type:

regsvr32.exe  [path of HTML2XHTMLLib.dll]

Now you can use can reference the library in our application using the Add Reference feature.

The following code reads the html file and converts it into the XHTML file and save it with the different name:

public void UsingHTMLTOXHTMLConverter(string html)
{
FileStream fs = null;
StreamWriter sw = null;

XHTMLUtilities xhtmlUtil = new XHTMLUtilities();
string xhtml = xhtmlUtil.convertToXHTML(html);

Label1.Text = Server.HtmlEncode(xhtml);

try
{
// write a new html file which contains the xhtml code

fs = File.Create(Server.MapPath("TestingResult.html"));
sw = new StreamWriter(fs);
sw.WriteLine(xhtml);

}
catch (Exception ex)
{

}
finally
{
// close the filestream and close the writer
sw.Close();
fs.Close();

}

}