<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<asp:xml id="Xml1" runat="server" transformsource="~/Data.xslt"/>
File:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connStr = "database=Northwind;Data Source=.\\SQLEXPRESS;User id=Tom;pwd=password";
XmlDocument x = new XmlDocument();
XPathNavigator xpathnav = x.CreateNavigator();
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand command = new SqlCommand("select * from Customers as Customer for XML AUTO, ELEMENTS", conn);
using (XmlWriter xw = xpathnav.PrependChild())
{
xw.WriteStartElement("Customers");
using (XmlReader xr = command.ExecuteXmlReader())
{
xw.WriteNode(xr, true);
}
xw.WriteEndElement();
}
}
Xml1.XPathNavigator = xpathnav;
}
}
File: Data.xslt
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<h3>List of Customers</h3>
<table border="1">
<tr>
<td>Company Name</td><td>Contact Name</td><td>Contact Title</td>
</tr>
<xsl:apply-templates select="//Customer"/>
</table>
</xsl:template>
<xsl:template match="Customer">
<tr>
<td><xsl:value-of select="CompanyName"/></td>
<td><xsl:value-of select="ContactName"/></td>
<td><xsl:value-of select="ContactTitle"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
|