Xml to xslt transformation using .net


This code snippet deals with transforming XML to Xslt using .net XSL is an abbreviation for Extensible style sheet and the XSlt stands for XSL Transformation this article also contains the reason for using the appropriate keywords etc

XSL-Extensible Stylesheet Language


This is a code sample for transforming Xml to xslt using C# and .NET.
The XSLT is an abbreviation for XSL Transformation
(For basic structures of xml and xslt please refer to the attachment)

In the Xslt, I have used a key word value-of this is nothing but used to extract the value of a node and i have used which as its name suggests used to sort the items in either ascending or descending order

The better way of using sort and xsl apply-template is explained here
refer:http://msdn.microsoft.com/en-us/library/ms256045.aspx

Xslt Transformation



The above xml file gets transformed as follows


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Xml.Xsl;
using System.Web.UI.WebControls;
using System.Xml.XPath;
using System.IO;

public partial class xsltTransformation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xmlFilePath = Server.MapPath("xmlfiles/Sample.xml");
string xsltFilePath = Server.MapPath("xmlfiles/Test.xslt");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlFilePath);
MemoryStream str = new MemoryStream();
// Load XSLT file to transform
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(xsltFilePath);
transformer.Transform(xDoc, null, str);
using (StreamReader reader = new StreamReader(str))
{
str.Position = 0;
Response.Write(reader.ReadToEnd());
reader.Close();
}
}
}


I have used xml document but it is better to proceed with xpathnavigator since it gives a better performance in the long run


Attachments

  • sample Xslt and xml for reference (43859-231534-sample-Xslt-xml-reference.rar)
  • Comments

    Guest Author: Susan11 Jun 2012

    xml is typically used to tnresfar data on the internet via http. i would recommend using a local database to store date instead of xml. make sure you know how to design a relational database.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: