Hi there. The project I am building is an ASP.NET browser based application that has two classes.
Login class
main menu class
now these two classes are working as they should (I.e. valid user logs in, proceed to main menu). The trouble is, I also have another class that applies an XSLT to a user specified (Via main menu) XMLfile. the user navigates to an XML file (on hard disk) then specifies an output directory (browsing again) to store the output. there is a submit button the user hits when they are ready to transorm the XML. the trouble I am having is that i do not know how to transmit the data from the two browsers (XML File locator and output file locator) to the XSLT class. i.e. I do not know how to specify to my transformer class which XML is to be used.
Any suggestions?
thanks for your time!

Recommended Answers

Now I am a bit confused... is the file saved on your server or on the clients' machines? If you are only redirecting the save, then you cannot apply the XSLT. But if you are saving it to your server, then you can apply it. A workaround for this is …

Jump to Post

Yeah no problem. The reason why you cannot do it the other way is that the only way you can modify a file is that if it gets to your server and you can modify it there. Otherwise, the client would have to have the XSLT file (I think its …

Jump to Post


And no, it does not restrict anything. The thank you page could easily just say "file converted successfully. Your download should start in a few seconds. If you want …

Jump to Post

nope, just switch the two lines.

You appended the two strings together, then changed the file extension of one of the strings.

You need to change the file extension, THEN append :)

Jump to Post

because it's not the same name anymore!

Declare savepath outside your function so it's a global variable, or throw it in with your "ApplyXSLTransformation()" to pass the new file name. I would recommend adding an argument to


And just change all fileupload1.filename (WITHIN ApplyXSLTransformation) to savepath

Jump to Post

All 85 Replies

forgot to add, I am using C# for scripting/transform class.

Right, an update. have managed to get the file upload to save a file (copy) specified by a user (via the browse button) to a directory (specified by the user via a textbox).Now this obviously only copies the file. what I need is to learn is how to apply the XSLT before the file is output. Any ideas folks? thanks

having thought about this, the file upload box will only COPY a file to a web server. it doesnt cater for CREATION of files. therefore, I will be able to apply an XSLT to an XML file I upload, but I will not be able to save the modified version. Is that correct ?

Now I am a bit confused... is the file saved on your server or on the clients' machines? If you are only redirecting the save, then you cannot apply the XSLT. But if you are saving it to your server, then you can apply it. A workaround for this is that first they upload the file to you, you save it in a temp directory on the server, apply the XSLT, then send it back to the client and delete your copy on the server.

Is that what you are trying to do?

that is essentially what I need to do yes, but I went down the wrong way with this.I initially thought I could save it to their machine and transform it before the save was established. Saving it in a temp dir, apply XSLT send to client. That is an excellent idea, I hadn't thought of that. I have been hell bent on getting it to work the other (wrong) way lol. I'll give it a bash and let you know how it goes SheSaidImaPregy.Thanks for your reply

Yeah no problem. The reason why you cannot do it the other way is that the only way you can modify a file is that if it gets to your server and you can modify it there. Otherwise, the client would have to have the XSLT file (I think its a file, or XSL) on their computer for you to apply it.

commented: Always helpful and eager to aid! +1

of course! that makes perfect sense now. thanks, I was thinking might there be security implications if a server could transform an uploaded file before it saves it back to your hard drive?

Yup, it is not allowed. Otherwise, it is called hacking and is illegal. Its fun, but not cool for the person being hacked! lol

ok so the plan is to upload file to server, write code to save file on server, perform transformation on that file on server then send transformed file (from server) back to client.Finally remove original xml file from server lol seems like a lot of work is there no easier way ?

also, how do I create a DOWNLOAD/SAVE AS dialog button so a user (client) can specify where they want to save file on hard drive ? Visual C# has a control for a file upload box but not a download box - which I think is odd

nope. no easier way. But matter of fact.. it is quite easy! You do a simple save, the open the file, rewrite it, then save it again.. then send it back and delete. Only hard part is the rewriting, but if you can do that then it is easy!

most people don't upload then download right away :)

so no there is no way to modify the upload button/box unless you deal with java that runs on the clients' platform.

ok thanks a lot for all your help with this, will knuckle down and let you know how it goes!

how would I get the XML file transformed server side, have been researching this and I cannot find much info about it. any ideas?

hi there SheSaidImaPregy, that tutorial you advised I look at is very helpful but it deals mainly with databases and aspnet. I am not using a database system in this project, I just need the user to be able to specify an xml file to upload to server, server XSLT that xml to produce HTML (I have written the XSLT). The user can then download this HTML file to their local hard drive. All this needs to be done via a web browser (ASP.net).THis project is tough but I am not going to give up on it!Thanks again for all your help

Right here is my code behind as it stands:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

namespace runXslt { }

public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)
        // Specify the path on the server to
        // save the uploaded file to.
        String savePath = "C:/temp/";     

        // Get the name of the file to upload.
        String fileName = FileUpload1.FileName;

        // Get the extension of the uploaded file.
        string extension = System.IO.Path.GetExtension(fileName);

        // Before attempting to perform operations
        // on the file, verify that the FileUpload 
        // control contains a file.
        if (FileUpload1.HasFile)
            if ((extension == ".xml"))

                //string xmlPath = Server.MapPath(fileName);
                //string xslPath = Server.MapPath("test.xsl");

                // Append the name of the file to upload to the path.
                savePath += fileName;
                // Call the SaveAs method to save the 
                // uploaded file to the specified path.
                // If a file with the same name
                // already exists in the specified path,  
                // the uploaded file overwrites it.
                // Notify the user of the name of the newly saved file
                Label1.Text = "Your file was uploaded as " + fileName;

                //call XSLT transform

                // Notify the user why their file was not uploaded.
                Label1.Text = "Your file was not uploaded because " + 
                    fileName + "it does not have a .xml extension.";


            // Notify the user that a file was not uploaded.
            Label1.Text = "You did not specify a file to upload.";
    protected void LinkButton1_Click(object sender, EventArgs e)

    private string ApplyXSLTransformation()
        string strHtml;

        string strXstFile = Server.MapPath("test.xslt");
        XslCompiledTransform x = new XslCompiledTransform();

        // Load the XML 
        XPathDocument doc = new XPathDocument(Server.MapPath("C:/temp/" + FileUpload1.FileName));

        // Load the style sheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        MemoryStream ms = new MemoryStream();
        XmlTextWriter writer = new XmlTextWriter(ms, null);
        StreamReader rd = new StreamReader(ms);
        xslt.Transform(doc, writer);
        ms.Position = 0;
        strHtml = rd.ReadToEnd();
        return strHtml;

red is where I am getting an "invalid virtual path warning".says C:/temp/library.xml is invalid. Also, am I going about this the right way <from looking at my code>?thanks

that error is a runtime error that occurs as I am debugging - forgot to add that!

This is why buddy:

XPathDocument doc = new XPathDocument(Server.MapPath("C:/temp/" + FileUpload1.FileName));

If you read the code, you are trying to find the server's physical path of C:/temp/... however, you already have the physical path and the server can't find the physical path of your starting drive. If you wish to use Server.MapPath, change "C:/temp/..." to "/temp/..."


ok thanks for that suggestion, only trouble is now I have changed it to

(Server.MapPath("/temp/" + FileUpload1.FileName));

but now I am getting a runtime error of "failed to map the path "/temp/****"". where **** is the filename I have uploaded. Hmmmm

is temp in the root of your server? How about you just ditch server.mappath or comment it out for now, and just put the direct URL in there:

(("C:/temp/" + FileUpload1.FileName));

ok I shall give that a try. At the moment I am using just one machine to test & deploy this project. So it is not on a server, is on my local machine. Thanks again for your help, its very much appreciated.

right I'm not sure but I think that is working OK now. I will be glad to get this fixed its a tough project this one, it is interesting though plus I am learning a lot!

That's why your servermappath isn't working is because it is on your machine, not on a web server. But if you wish, add an try catch there. Try for the server.mappath, catch and do your server path :)

Its certainly copying the files over fine now but whether or not it is XSLT-ing them correctly I do not know as I am using a mock stylesheet for now.
OK so now those files have been transformed (I think) how can a user download them from the server(os so to speak). Is it not odd that there is a visual studio control for upload file but not download file ?

You execute the page of the directory the file is at. Download controls aren't really needed, that's why it isn't included (plus they are tricky). You should be able to just use server.transfer to grab the file like this:


and it should automatically appear for download. If it doesn't let me know and I'll let you know of an alternate source.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.