Re: Display XML Output from SQL Server
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Fri, 26 May 2006 01:55:30 GMT
Hi Terry,
Did you see the xslt file and test page 's codebehind code in my last
reply? That page is quite simple and you just need to put the same
codebehind(page_load code) into your own empty aspx page. Also, as I said I
used the Northwind test database so as to make the test simplified. Anyway,
I'll repaste the test code snippet below in case you didn't see them in the
original thread:
==========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 ...)
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.)
.
- 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
- From: Steven Cheng[MSFT]
- Re: Display XML Output from SQL Server
- From: Terry Holland
- Re: Display XML Output from SQL Server
- Prev by Date: Re: question
- Next by Date: Can not get IIS to run ASPX when I am connecting to SQL DB.
- Previous by thread: Re: Display XML Output from SQL Server
- Next by thread: Re: Display XML Output from SQL Server
- Index(es):
Loading