Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

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:

http://www.InfoPathDev.com.

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:

C:\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:

 // This method uses the library to convert the html document to Xhtml document
    
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();
           
            
        }      
        
    }

powered by IMHO

 

Posted on Wednesday, October 26, 2005 11:02 PM | Back to top


Comments on this post: Converting HTML to XHTML

# re: Converting HTML to XHTML
Requesting Gravatar...
Thanks for this! Do you also know if there is a good utility that does this from the command line. While this info is good to know, it would be nice not to go through the effort of wrapping in an all purpose "shell" and building an install package to be truly generic. Especially if there's something already that does this.
Left by John Watson on Oct 27, 2005 6:12 AM

# re: Converting HTML to XHTML
Requesting Gravatar...
Hmm, well I guess you can make a small console application and do the same which I have demonstrated above.
Left by Azamsharp on Oct 27, 2005 7:47 PM

# re: Converting HTML to XHTML
Requesting Gravatar...
Thanks for code , result will be xhtml 1.1 ?
Left by Kenny on Jul 12, 2006 12:32 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net