Search Forum
(57250 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

XSLT / XML / C#
By Barkha Moryani

Hello everyone. In this source code I am going to show how
to apply xslt to data coming from a database in xml format.

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;

public class XsltTransform
{
	public static void Transform()
	{
		SqlConnection nwindConn = new SqlConnection("Data Source=INMUMIS123;database=northwind;uid=sa;pwd=;");
		nwindConn.Open();

		DataSet custDS = new DataSet("CustomerDataSet");

		SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", nwindConn);
		custDA.Fill(custDS, "Customers");

		SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders", nwindConn);
		ordersDA.Fill(custDS, "Orders");

		nwindConn.Close();

		custDS.Relations.Add("CustOrders",
			custDS.Tables["Customers"].Columns["CustomerID"],
			custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

		XmlDataDocument xmlDoc = new XmlDataDocument(custDS); 
      
		XslTransform xslTran = new XslTransform();
		xslTran.Load("transform.xsl");
             
		// This is for generating the output in new html
		XmlTextWriter writer = new XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8);
		writer.Close();

		// This is for writing in the current page
		xslTran.Transform(xmlDoc, null, Response.OutputStream);
	
	}
}
This class XsltTransform connects to database fills the data in
a XmlDocument and then apply it the Xslt already present with me.
<%@ Page language="c#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript (ECMAScript)">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<script>
	public void Page_Load(object sender, System.EventArgs e)
	{
		Transform();
	}
	public void Transform()
	{
		SqlConnection nwindConn = new SqlConnection("Data Source=INMUMIS123;database=northwind;uid=sa;pwd=;");
		nwindConn.Open();

		DataSet custDS = new DataSet("CustomerDataSet");

		SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", nwindConn);
		custDA.Fill(custDS, "Customers");

		SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders", nwindConn);
		ordersDA.Fill(custDS, "Orders");

		nwindConn.Close();

		custDS.Relations.Add("CustOrders",
			custDS.Tables["Customers"].Columns["CustomerID"],
			custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

		XmlDataDocument xmlDoc = new XmlDataDocument(custDS); 
      
		XslTransform xslTran = new XslTransform();
		xslTran.Load("transform.xsl");
             
	//	XmlTextWriter writer = new XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8);

		xslTran.Transform(xmlDoc, null, Response.OutputStream);
	//	writer.Close();
	}
	</script>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
		</form>
	</body>
</HTML>
The above aspx page makes the object of the class and
XsltTranform and calls the Transform function.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="CustomerOrders">
  
  <STYLE>
  BODY {font-family:verdana;font-size:9pt}
  TD   {font-size:8pt}
  </STYLE>

    <TABLE BORDER="1">
      <xsl:apply-templates select="Customers"/>
    </TABLE>


</xsl:template>

<xsl:template match="Customers">
    <TR><TD>
      <xsl:value-of select="ContactName"/>, <xsl:value-of select="Phone"/><BR/>
    </TD></TR>
      <xsl:apply-templates select="Orders"/>
</xsl:template>

<xsl:template match="Orders">
  <TABLE BORDER="1">
    <TR><TD valign="top"><B>Order:</B></TD><TD valign="top"><xsl:value-of select="OrderID"/></TD></TR>
    <TR><TD valign="top"><B>Date:</B></TD><TD valign="top"><xsl:value-of select="OrderDate"/></TD></TR>
    <TR><TD valign="top"><B>Ship To:</B></TD>
        <TD valign="top"><xsl:value-of select="ShipName"/><BR/>
        <xsl:value-of select="ShipAddress"/><BR/>
        <xsl:value-of select="ShipCity"/>, <xsl:value-of select="ShipRegion"/>  <xsl:value-of select="ShipPostalCode"/><BR/>
        <xsl:value-of select="ShipCountry"/></TD></TR>
  </TABLE>
</xsl:template>

</xsl:stylesheet>
And this was the xslt file.

Happy Coding!!!