right ok, I am not sure where to put that code and do I have to declare newsavedfile as the file I just uploaded?

newsavedfile should be the file path of your newly saved file. Then, when you are done with all your code, put that there so it automatically has them download it.

do you mean
XPathDocument doc = new XPathDocument(Server.MapPath("C:/temp/" + FileUpload1.FileName));
instead use
server.transfer(server.mappath(newsavedfile)
)

I dont really understand :-/

it would be very good if the user auto downloaded the files to a predefined folder(created if not existing) without the user having to click a button if you know what I mean. But then there are security issuesI guess

Yes you won't be able to do that.

No, what I mean is that after you are done saving and formatting the XML document, compeletely done with it, add that code there, or in your case:

Server.Transfer("C:/temp/" + FileUpload1.FileName)

That should basically be your last line of code after converting everything.

hmmm. Having done that, getting runtime exception of "expected virtual path"
I put that line right before the return strHtml statement at bottom of code.lol this is my first asp.net app - talk about jumping in at deep end !

You are receiving that error on the code "C:/..."? That's odd, but okay. So put the server.mappath("") relative to your document. If your document is in C:/documents/asppage.aspx then put something like this:

server.transfer(server.mappath("../temp/" & FileUpload1.FileName))

Oh, and keep in mind that if "filename" is only the name, you will have to add ".xml" at the end. Spit out (server.mappath("../temp/" & FileUpload1.FileName)) to see if it is displaying correctly. (put it in a response.write)

its still throwing that exception when I change it to server.transfer(server.mappath("../temp/" & FileUpload1.FileName))

OK here is what I currently have

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"))
            {                     
                // 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.
                FileUpload1.SaveAs(savePath);
           
                // Notify the user of the name of the newly saved file
                Label1.Text = "SUCCESS! - Your file was uploaded and converted as " + fileName;
                

                //call XSLT transform
                ApplyXSLTransformation();
            }

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

            }
        }

        else
        {
            // Notify the user that a file was not uploaded.
            Label1.Text = "ERROR - 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 - use (Server.MapPath("/temp/" + FileUpload1.FileName));
        //if using server i.e. not local machine
        XPathDocument doc = new XPathDocument(("C:/temp/" + FileUpload1.FileName));

        // Load the style sheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(strXstFile);
        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();
        rd.Close();
        ms.Close();
        Server.Transfer(Server.MapPath("C:/temp/dl" + FileUpload1.FileName));
        return strHtml;
       
    }
}

try specifying the name of the file. server.transfer may not allow transfering to a XML file, you know? It may only except .net extensions.'

You can try response.redirect instead, or build a "thank you" page with the download link where they can download the file. Then they can click and download it when they are ready.

right I understand ok. But wont the thank you page restrict this project to only allowing one xml to be transformed per session ?

tried to response.redirect, gettting same exception when debugging. odd. man I owe you some beers!

right ok...thanks for the links I have skimmed through them. So a good idea is to maybe let a user transform as many files as they want then maybe have a CLOSE button (or whatever) that brings them to a page where they download the transformed files?

or they can just repeat the process.

ok man thanks a million for all your help with this. Iill let you know how I get on with it. I think I will use a button on the same page that I shall call DOWNLOAD where a user can choose to download the files to where they want (like a dialog box).

got hold of the correct XSLT now, but it doesnt seem to be Transforming the xml files at all. Now I know the XSLT is correct as I have checked it over several times. Is the code wrong somewhere?

hmm Ill give it a bash now and sign off will let you know how it goes. many thanks again for the help its great

Right ok, I think I have a good idea as to what the problem is. I have saved the file before I have applied the XSLT (duh!lol) but also I am not sure the fileupload box is the best way to go about this. I think this because I cannot think of a way to transform the XML before it is saved? Any ideas ?

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"))
            {                     
                // 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.
                FileUpload1.SaveAs(savePath);
           
                // Notify the user of the name of the newly saved file
                Label1.Text = "SUCCESS! - Your file was uploaded and converted as " + fileName;
                

                //call XSLT transform
                ApplyXSLTransformation();
            }

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

            }
        }

        else
        {
            // Notify the user that a file was not uploaded.
            Label1.Text = "ERROR - 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 - use (Server.MapPath("/temp/" + FileUpload1.FileName));
        //if using server i.e. not local machine
        XPathDocument doc = new XPathDocument(("C:/temp/" + FileUpload1.FileName));

        // Load the style sheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(strXstFile);
        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();
        rd.Close();
        ms.Close();
        Server.Transfer(Server.MapPath("C:/temp/dl" + FileUpload1.FileName));
        return strHtml;
       
    }
}

Right, I have now got the XSLT to transform the XML to HTML. I am also able to store this new HTML file to a directory, (not specifiable by user...yet).It was as I said, I had saved the file but not writetn the newly created HTML to it!Great! now I have to concentrate on geting the newly created HTML file to have the extension .html not .xml. This I will investigate!

you just save it with .html instead of .xml. Okay, this is what you need to do.

1. Save the file
2. Open/Apply the XSLT to the .xml file
3. Save the file AS filename.html
4. Create download link to file location.

ok thanks, just getting on to the extension renaming part now.

I had thought it was as simple as

fileName.ReplaceExtension(".html");

But I was wrong lol!

no no lol, you need to use "saveAs" and save it with the name and the extension .html. Do something like, find the period in the string with InStr, then get the left number characters from that dot (InStr - 1), then "SaveAs(filename & ".html")"

right ok. that does sound difficult, I know nothing about InStr for starters lol

what would be ideal is if the user is forced to just save the file in a hard coded directory (created if not existing) as html file. Now I have achieved half of that goal! the html is saved to (say c:/temp) hard coded dir but its saved as .xml not .html.all i want is to change the file extension before it is saved in c:/temp. Any ideas? THanks

You can. Right where you have "saveAs" you are saving it.

InStr is just In String. It finds the first position of a character or phrase.

So lets say the filename is:
Dim filename As String = "yucky.xml"

You use InStr to find it:
InStr(filename, ".xml")

Or use a huge one and just do everything at once:

.SaveAs((filename.remove((InStr(filename, ".xml")),(Len(filename)-(InStr(filename,".xml"))))) & ".html")

ok man, thanks for all your help with this you are a star!

hey I am using c#, whats the equivalent for InStr in c#

Be a part of the DaniWeb community

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