I’ve got an XML document, which Im trying to add an xslt to in c# and output to screen. I’ve been doing some research on this and it looks like I need to use the XslCompiledTransform class to do this.
The problem is, when I call the Load method,my page errors. All the samples I have found online seem to suggest I just call the load like “Sort.xsl” – which is in the same folder as this file. Im also assuming I need to use the MemoryStream() to display the transformed xml to screen?
Im using the XmlDocument to do all my processing and all data is in “xmlDocument”. Can someone help me load the external xsl, apply it and the display results to screen.
Many thanks
// Create a writer for writing the transformed file.
MemoryStream strm = new MemoryStream();
XmlWriter writer = XmlWriter.Create(strm);
// Create and load the transform with script execution enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
transform.Load(@"Convert.xsl", settings, null);
// Execute the transformation.
transform.Transform(xmlDocument, Response.OutputStream);
I am assuming you are writing a Web application, and ‘output to screen’ means sending the transformed XML to the browser.
The current directory is that of the IIS user – i.e. the wrong one.
It is necessary to compose the path of the XSL file using the actual (physical) path of the application – accessible using `HttpRequest.PhysicalApplicationPath’ – something like:
Note that this works only if the code is called while serving a request – otherwise
HttpContext.Currentisnull, and assumes thatConvert.xslis in the main application directory (together with the .ASPX files etc).