User Id :    Password :      New Member   Forgot Password  
 
Use XSL in csharp
Description Use XSL in csharp   No. of Views     2301
  Rating     0
Author Gaurav   Posted On     16 Feb 2014
Tags XML,Xslt,XML : General    

Sample Code   Download Code

XSL stands for EXtensible Stylesheet Language.
 
XSLT stands for XSL Transformations to other formats like XHTML,Xaml etc. 
 
The following program reads Employee.xml and Employee.xsl file.
 
It creates Employee.htm file as a output file.
 
In this article <xsl:for-each> use which allows you to do looping in XSLT.
 
Employee.XML
 
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Employee.xsl"?>
<Employee>
  <emp>
    <name>Gaurav</name>
    <age>29</age>
    <designation>TA</designation>
    <address>Delhi</address>    
  </emp>
  <emp>
    <name>Nilesh</name>
    <age>24</age>
    <designation>SE</designation>
    <address>Pune</address>
  </emp>
  <emp>
    <name>Santosh</name>
    <age>25</age>
    <designation>SSE</designation>
    <address>Mumbai</address>
  </emp>
  <emp>
    <name>Girish</name>
    <age>26</age>
    <designation>TA</designation>
    <address>Noida</address>
  </emp>
</Employee>
 

Employee.XSLT

 
<?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:template match="/">
      <html>
        <body>
          <h2>Employee Details</h2>
          <table border="1">
            <tr>
              <th>Name</th>
              <th>Age</th>
              <th>Designation</th>
              <th>Address</th>
            </tr>
            <xsl:for-each select="Employee/emp">
              <tr>
                <td>
                  <xsl:value-of select="name"/>
                </td>
                <td>
                  <xsl:value-of select="age"/>
                </td>
                <td>
                  <xsl:value-of select="designation"/>
                </td>
                <td>
                  <xsl:value-of select="address"/>
                </td>
              </tr>
            </xsl:for-each>
          </table>
        </body>
      </html>
    </xsl:template>
  </xsl:stylesheet>
 
C # code to transform the xml to html.
 
 

using System.Xml;
using System.Xml.Xsl;


private void Form1_Load(object sender, EventArgs e)
        {
            string xmlPath = "D:\\SamplePractice\\Xslt\\Xslt\\Xslt\\Employee.xml";
            string xslPath = "D:\\SamplePractice\\Xslt\\Xslt\\Xslt\\Employee.xslt";
            string resultPath = "D:\\SamplePractice\\Xslt\\Xslt\\Xslt\\Employee.htm";
            //Load XML file
            XmlReader XMLreader = XmlReader.Create(xmlPath);
            //Creats output file
            XmlTextWriter XMLwriter = new XmlTextWriter(resultPath, null);           
            //Loads XSLT file
            XslCompiledTransform xsl = new XslCompiledTransform();
            xsl.Load(xslPath);
            xsl.Transform(XMLreader, XMLwriter);
            XMLwriter.Close();
        }
 
 
The Employe.htm file will look like :
 
 
 

About Author

About Author I am as simple as c#. Gaurav
No Photo
 
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Gaurav on 02 Sep 2015 at 11:49 AM
 

 good

 
 
   
Write your comment here.
Comment
Verification Code