0

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);
2
  • 1
    You forgot to post error...along with your question... Commented Aug 29, 2012 at 13:18
  • Apologies, error it says System.IO.FileNotFoundException: Could not find file "c:\windows\system32\inetserv..." It does exist, in the same folder I am calling it from, however Load method seems to be looking on c drive? Commented Aug 29, 2012 at 14:38

1 Answer 1

1

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:

transform.Load(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,"Convert.xsl"), settings, null); 

Note that this works only if the code is called while serving a request - otherwise HttpContext.Current is null, and assumes that Convert.xsl is in the main application directory (together with the .ASPX files etc).

Sign up to request clarification or add additional context in comments.

3 Comments

Hi MiMo, thanks, tried this, intellisense is coming up with a red line underneath. Error: No overload for method Combine - takes 4 arguments
Sorry, I missed a closed parenthesis after "Convert.xsl" - now fixed.
...are my assumptions correct? You did not specify them in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.