Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Html to Xhtml conversion in c#
11-21-2012, 06:56 PM
Post: #1
Html to Xhtml conversion in c#
I created a simple program to convert a html document to xhtml. if you want to know the difference between them, you can check below link
Difference between html and xhtml

I used html tidy for conversion.
Tidy is an open source program and library for checking and generating clean XHTML/HTML.

I put the tidy executable file in solution directory and then execute its command for conversion
tidy -asxhtml -numeric < index.html > index.xhml

i dynamically created this command in batch file based on selected html file path and after creating in batch file, at the end executed the batch file for conversion

Code for browsing html file
private void BtnBrowse_Click(object sender, EventArgs e)
            OpenFileDialog browseFile = new OpenFileDialog();
            browseFile.Filter = "Html Files (*.html)|*.html";
            browseFile.Title = "Browse Html file";
            if (browseFile.ShowDialog() == DialogResult.Cancel) return;
                txtPath.Text = browseFile.FileName;
            catch (Exception)
                MessageBox.Show("Error opening file", "File Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

Code for modifying and executing batch file
private void Modify_And_Execute_Batch_File(string Path)
            StreamWriter SW = null;
            if (File.Exists(GetFilePath() + "\\execute.bat"))
                File.Delete(GetFilePath() + "\\execute.bat");
            SW = File.CreateText(GetFilePath() + "\\execute.bat");
            string str_Command = "tidy -asxhtml -numeric <" + Path + ">  " + Path.Replace(".html", ".xhtml") + "";

            System.IO.File.WriteAllText("execute.bat", str_Command);

            //execute batch file after creation

            //please comment below line, i forgot to comment in attached code :)

        public void Execute_Batch_File()
            Process p = null;
                string targetDir;
                targetDir = string.Format(GetFilePath());
                p = new Process();
                p.StartInfo.WorkingDirectory = targetDir;
                p.StartInfo.FileName = "execute.bat";
                p.StartInfo.CreateNoWindow = false;
            catch (Exception ex)
                Console.WriteLine("Exception Occurred :{0},{1}",
                            ex.Message, ex.StackTrace.ToString());

At the end newly created xhtml file will generate on the same place where your html exist. so make sure you have write access to that particular directory.

complete code is attached with sample html file which i tested. please let me know if you have any issue in executing or understand the code.

Reference Link:

Attached File(s)
.html  index.html (Size: 845 bytes / Downloads: 10)
.zip  Html2Xml Conveter.zip (Size: 161.18 KB / Downloads: 29)

[Image: 2604595733.png]
05-17-2015, 11:38 PM
Post: #2
RE: Html to Xhtml conversion in c#
Perfect mate.. I am back in action.. Smile

Thanks and Regards

Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Html to pdf conversion in c# nisar87 13 42,368 09-06-2016 10:30 AM
Last Post: evopdf
  pdf to html conversion in asp.net c# nisar87 9 8,867 04-12-2016 12:21 PM
Last Post: BenJobs
  Html to Pdf conversion in asp.net c# nisar87 7 55,452 06-17-2014 06:31 AM
Last Post: dev_chirs
  Html to image conversion c# nisar87 0 9,110 05-06-2012 01:22 PM
Last Post: nisar87

User(s) browsing this thread: 1 Guest(s)