Convert an XML into HTML Table using LINQ-to-XML

This blog explains

  • How read the XMl File
  • How to convert an xml data as html table.

To demonstrate the above, i am using the following Xml script which contains employee Department, Name, Gender, Salary tags.

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
  2. <Employees>    
  3.   <Employee Department = "IT">    
  4.     <Name>Jaipal</Name>    
  5.     <Gender>Male</Gender>    
  6.     <Salary>18000</Salary>    
  7.   </Employee>    
  8.   <Employee Department = "Accounts">    
  9.     <Name>Jayanth</Name>    
  10.     <Gender>Male</Gender>    
  11.     <Salary>12000</Salary>    
  12.   </Employee>    
  13.   <Employee Department = "HR">    
  14.     <Name>Tejaswini</Name>    
  15.     <Gender>Female</Gender>    
  16.     <Salary>15000</Salary>    
  17.   </Employee>    
  18. </Employees>    
After converting the above XML file it should looks as follows.
  1. <table border="1">    
  2.   <thead>    
  3.     <tr>    
  4.       <th>Department</th>    
  5.       <th>Name</th>    
  6.       <th>Gender</th>    
  7.       <th>Salary</th>    
  8.     </tr>    
  9.   </thead>    
  10.   <tbody>    
  11.     <tr>    
  12.       <td>IT</td>    
  13.       <td>Jaipal</td>    
  14.       <td>Male</td>    
  15.       <td>18000</td>    
  16.     </tr>    
  17.     <tr>    
  18.       <td> Accounts </td>    
  19.       <td>Jayanth</td>    
  20.       <td>Male</td>    
  21.       <td>12000</td>    
  22.     </tr>    
  23.     <tr>    
  24.       <td>HR</td>    
  25.       <td>Tejaswini</td>    
  26.       <td>Female</td>    
  27.       <td>15000</td>    
  28.     </tr>    
  29.   </tbody>    
  30. </table>    

Steps: To transform an Xml to HTML table

The html table contains <table>, <thead>, <th>, <tbody>, <tr>, <td> and </table>, </thead>, </th>, </tbody>, </tr>, </td> tags. 
  1. Read the XML file.

    XDocument xmlDocument = XDocument.Load(@"D:\Jaipal Reddy\Expert-Exchange\LinqToXML\XmlData.xml"); 

  2. Add table as a root element, if you want to add border attribute just use XAttribute.

  3. Add thead, tbody as child elements to the table element.

  4. Add tr as child element to the thead and set the column names by adding th as child element to the tr.

  5. Same way add tr as child element to the tbody , and next read and populate Xml data with td. 
Here is the Complete Code .
  1. static void Main(string[] args)    
  2. {    
  3.     XDocument xmlDocument = XDocument.Load(@"D:\Jaipal Reddy\Expert-Exchange\LinqToXML\XmlData.xml");    
  4.   
  5.     XDocument result = new XDocument    
  6.         (new XElement("table"new XAttribute("border", 1),    
  7.                 new XElement("thead",    
  8.                     new XElement("tr",    
  9.                         new XElement("th""Id"),    
  10.                         new XElement("th""Name"),    
  11.                         new XElement("th""Gender"),    
  12.                         new XElement("th""Salary"))),    
  13.                 new XElement("tbody",    
  14.                     from emp in xmlDocument.Descendants("Employee")    
  15.                     select new XElement("tr",    
  16.                                 new XElement("td", emp.Attribute("Id").Value),    
  17.                                 new XElement("td", emp.Element("Name").Value),    
  18.                                 new XElement("td", emp.Element("Gender").Value),    
  19.                                 new XElement("td", emp.Element("Salary").Value)))));    
  20.   
  21.     result.Save(@"D:\Jaipal Reddy\Expert-Exchange\LinqToXML\HtmlResult.htm");    
  22. }  

I hope you enjoyed it. Please provide your valuable suggestions and feedback.