---
title: "Convert Data Table to XML, XSD, and HTML"  
description: "In this blog I am trying to explain the concept of Convert Data Table to XML, XSD, and HTML.This involve following steps as: Step1:Let’s create a data"  
author: "Anonymous User"  
published: 2013-08-22  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/575/convert-data-table-to-xml-xsd-and-html  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# Convert Data Table to XML, XSD, and HTML

In this blog I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of Convert [Data Table](https://www.mindstick.com/forum/12752/how-to-select-specific-columns-from-a-data-table-in-vb-dot-net) to XML, XSD, and HTML.

This involve following steps as:

Step1:Let’s create a data table and add some rows and column on it.

```
DataTable dt = new DataTable()            {                TableName = "employee"            };
            DataColumn keyColumn = dt.Columns.Add("ID", typeof(System.Int32));
            dt.Columns.Add("Name", typeof(System.String));
            dt.Columns.Add("Desig", typeof(System.String));
            dt.Columns.Add("Address", typeof(System.String));
            dt.Columns.Add("MobileNo", typeof(System.String));
            dt.Columns.Add("gender", typeof(System.String));
            dt.PrimaryKey = new DataColumn[] { keyColumn };
            dt.Rows.Add(new object[] { 101," Ashish","programmer","25369874125","male"});
            dt.Rows.Add(new object[] { 102, " Shubham", "tester","2536568923","male"});
            dt.Rows.Add(new object[] { 103, " Anupam", "developer","9807167825","male"});
            dt.Rows.Add(new object[] { 104, " Anurag", "Sysanalyst","945632012","male"});
            dt.AcceptChanges();
Step2: Now convert data table into xmlusing (TextWriter writer = new StringWriter())
               {
                  dt.WriteXml(writer);                  var xml = writer.ToString();                  Console.WriteLine("-----------------xml format----------------------");
                  Console.WriteLine(xml);                 // following lines convert xml into html                   }
```

At this point of time you have successfully created the data table and convertint it into [xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp).

Now in order to convert [xml data](https://www.mindstick.com/forum/149/problem-in-showing-the-xml-data-in-table-form-on-browser) in to html we need to know basics of XSLT. [Introduction](https://www.mindstick.com/articles/13122/an-introduction-to-network-cables) to XSLT:\

XSLT is a [language](https://www.mindstick.com/startup/28/preply-the-fast-growing-platform-transforming-language-learning) for transforming XML [documents](https://www.mindstick.com/articles/156931/the-main-types-of-business-documents) into XHTML documents (i.e. it takes XML as input and convert it into XHTL/XML) or to other XML documents. It always separates the data from its formatting ([style sheet](https://www.mindstick.com/interview/1606/what-are-style-sheets)). The data is provided via XML and the formatting is decided in XSL. The traversing of XML is done using XPath. The following diagram depicts the [scenario](https://yourviews.mindstick.com/view/81221/sports-will-change-the-education-scenario):\

\

\
![Convert Data Table to XML, XSD, and HTML](https://www.mindstick.com/blogs/b7ee4938-530e-4aa4-80ab-c7c7bcf1062f/images/b2e9e533-ee42-4815-8e9a-fd7c81f14577.png)

\

XSLT is capable of programming as looping, branching with IF and

declaring variable etc. In other words XSLT allows you to define variables,

you can have loops, you can have condition checks, and many more...

So now it is the time to start to create XSLT file in your program.

Step4: Right click on your project in solution explorer and a XSLT file, give

name it as “Sample.xslt” and the default code will be display as follows:

```
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>  <xsl:template match="@* | node()">    <xsl:copy>      <xsl:apply-templates select="@* | node()"/>    </xsl:copy>  </xsl:template></xsl:stylesheet>
```

\

Step5: Add your codes in XSLT file as given

```
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
 <xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="@* | node()">
    <html>      <body>        <table>          <tr>            <xsl:for-each select="/*/node()">              <xsl:if test="position()=1">                <xsl:for-each select="*">                  <td>                    <xsl:value-of select="local-name()"/>                  </td>                </xsl:for-each>              </xsl:if>            </xsl:for-each>          </tr>          <xsl:for-each select="*">            <tr>              <xsl:for-each select="*">                <td>                  <xsl:value-of select="."/>                </td>              </xsl:for-each>            </tr>          </xsl:for-each>        </table>      </body>    </html>  </xsl:template></xsl:stylesheet>
```

Step6: Add a class in your project say”xltOperation.cs” and add code as follows

```
  using System.Xml.Linq;  using System.Xml;  using System.Xml.Xsl;
namespace DatatabletoXmlConversition{    class XltOperation    {           public string GetValue(string templatePath, string xmlString)           {               XDocument xmlObj = XDocument.Parse(xmlString);               XDocument result = GetResultXml(templatePath, xmlObj);              return(result==null)?string.Empty:result.Document.ToString();
            }
        private XDocument GetResultXml(string templatePath, XDocument xmlObj)           {
               XDocument result = new XDocument();               using (XmlWriter writer = result.CreateWriter())                {
                 XslCompiledTransform xslt = new XslCompiledTransform();                  xslt.Load(templatePath);                  xslt.Transform(xmlObj.CreateReader(), writer);                }
               return result;         }    }}
```

The **XslCompiledTransform** class in the **System.Xml.Xsl** namespace. This class transforms XML data using an XSLT stylesheet. We just have to use two methods of this class: **Load** and **Transform.** The Load () method loads the defined XSLT file into **XslCompiledTransformed** class object and method Transform () actually transform the XML file into output file.

Step6: Finally build and run your project. You will got output as following screen shows.

\
![Convert Data Table to XML, XSD, and HTML](https://www.mindstick.com/blogs/b7ee4938-530e-4aa4-80ab-c7c7bcf1062f/images/8fcd11ec-353a-4dd7-b611-85585373b220.png)

\

And HTML output in below the XML output as

\

![Convert Data Table to XML, XSD, and HTML](https://www.mindstick.com/blogs/b7ee4938-530e-4aa4-80ab-c7c7bcf1062f/images/4eec6776-e6e8-4a72-a246-57cb301ae2d9.png)

---

Original Source: https://www.mindstick.com/blog/575/convert-data-table-to-xml-xsd-and-html

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
