Converting HTML to XHTML - ASP.Net

Tuesday, June 30, 2009

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:

<Drive>:\The path to the folder which contains the dll\regsvr32.exe 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();

}

}

0 comments:

  © Blogger templates Newspaper by Ourblogtemplates.com 2008

Back to TOP