Re: Display XML Output from SQL Server
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Thu, 25 May 2006 12:10:44 GMT
Hey Terry,
I think Flinky has provided the pretty good code example(thought it is
using .net framework 2.0's class). And as for the article you mentioned, it
is using the original ASP+COM approach and we should use .net framework's
XML processing classes in our case. Anyway, I've built the following test
page for your reference (I used the Northwind db's Categories table to make
it simplified):
==========xslt file=============
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:style*** version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<td>CategoryID:</td>
<td><xsl:value-of select="/Categories/CategoryID/text()"/></td>
</tr>
<tr>
<td>CategoryName:</td>
<td><xsl:value-of select="/Categories/CategoryName/text()"/></td>
</tr>
<tr>
<td>Description:</td>
<td><xsl:value-of select="/Categories/Description/text()"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:style***>
===================================
==============aspx page codebehind========
public class transform1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
//Get XML data from database
string sqlxml = "select top 1 CategoryID, CategoryName, Description from
Categories FOR XML AUTO , Elements";
conn.Open();
SqlCommand comm = new SqlCommand(sqlxml, conn);
comm.CommandType = System.Data.CommandType.Text;
XmlReader xdr = comm.ExecuteXmlReader();
//load the xml from xmlreader into xmldocument for further processing
XmlDocument doc = new XmlDocument();
doc.Load(xdr);
xdr.Close();
conn.Close();
//create XslTransform instance for transforming
XslTransform xsl = new XslTransform();
xsl.Load(Server.MapPath("./template.xslt"));
//flush the transformed result into current page's response output stream
Response.ClearContent();
xsl.Transform(doc.CreateNavigator(), null, Response.Output,new
XmlUrlResolver());
Response.End();
}
...................
}
=============================
Also, in the above code I simply flush the transformed result directly into
response output, you can choose to flush it into anyother OutputStream you
like(e.g the StreamWriter or a filestream ...)
Hope this also helps.
Regards,
Steven Cheng
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
.
- Follow-Ups:
- Re: Display XML Output from SQL Server
- From: Terry Holland
- Re: Display XML Output from SQL Server
- References:
- Re: Display XML Output from SQL Server
- From: Martin Honnen
- Re: Display XML Output from SQL Server
- From: Terry Holland
- Re: Display XML Output from SQL Server
- From: Steven Cheng[MSFT]
- Re: Display XML Output from SQL Server
- Prev by Date: Re: Auto type feature
- Next by Date: view state
- Previous by thread: Re: Display XML Output from SQL Server
- Next by thread: Re: Display XML Output from SQL Server
- Index(es):