
July 7th, 2004, 09:16 PM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 9
Time spent in forums: 5 m 40 sec
Reputation Power: 0
|
|
Well I'm back just in case someone was looking to do this, I have found a solution using code rather than the built in XML Control. The greater part of the code for this solution was thanks to Sonu Kapoor at the www.asp.net forums. I just massaged it into shape for my use.
It goes a bit like this:
(There's no error checking so an XSL or XML error will crash the page)
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e){
//Define the parameters
string param1 = "PARAMETER 1";
string param2 = "PARAMETER 2";
if(!Page.IsPostBack){
XmlDocument doc = new XmlDocument();
doc.Load( Server.MapPath("~/xml/source.xml") );
XslTransform xslt = new XslTransform();
xslt.Load( Server.MapPath("~/xsl/stylesheet.xsl") );
//ADD PARAMETER
XsltArgumentList arglist = new XsltArgumentList();
arglist.AddParam("param1", "", param1);
arglist.AddParam("param2", "", param2);
MemoryStream ms=new MemoryStream();
xslt.Transform( doc, arglist, ms);
ms.Seek( 0, SeekOrigin.Begin );
StreamReader sr = new StreamReader(ms);
//Apply the results to lblOutput
lblOutput.Text = sr.ReadToEnd();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XML Test</title>
</head>
<body>
<asp:Label ID="lblOutput" Runat="server"></asp:Label>
</body>
</html>
It's a bit more involved than using:
Code:
<asp:Xml
ID="Xml1"
Runat="server"
DocumentSource="~/source.xml"
TransformSource="~/stylesheet.xsl">
</asp:Xml>
But... It lets you pass parameters to the stylesheet and provides all the more flexibility. I hope this helps someone else
Cheers,
Andrew
|