User Id :    Password :      New Member   Forgot Password  
Use XSL in csharp
Description Use XSL in csharp   No. of Views     3012
  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.
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Employee.xsl"?>


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">  
    <xsl:template match="/">
          <h2>Employee Details</h2>
          <table border="1">
            <xsl:for-each select="Employee/emp">
                  <xsl:value-of select="name"/>
                  <xsl:value-of select="age"/>
                  <xsl:value-of select="designation"/>
                  <xsl:value-of select="address"/>
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.Transform(XMLreader, XMLwriter);
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.  


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


Write your comment here.
Verification Code