0

so instead of
FileUpload1.SaveAs(savePath);
its:
FileUpload1.SaveAs((filename.remove((InStr(filename, ".xml")),(Len(filename)-(InStr(filename,".xml"))))) & ".html")

replacing InStr with IndexOf

0

well no.. cause you are using C#, you need to replace some stuff for syntax, like

Len(filename) needs to become filename.length, I am unsure about instr if it is even used in C#.

0

Right the problem is:
It is creating two files 1 xml file and 1 html file with the name

name.html.html

. why is this lol

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;
using System.Text;


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;
                fileName = Path.ChangeExtension(fileName,".html");
                               
                // 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);

                
                //call XSLT transform
                String result = ApplyXSLTransformation();

                //write HTML from stream to file
                FileStream fs = new FileStream("C:/temp/" + fileName+".html", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(result);
                //result = Path.ChangeExtension(fileName, ".html");
                
                //result.ReplaceExtension(".html");
 
                //change from .xml to .html extension
                

                // Notify the user of the name of the newly saved file
                Label1.Text = "SUCCESS! - Your file was uploaded and converted as " + fileName;
                

                
            }

            else
            {
                // Notify the user why their file was not uploaded.
                Label1.Text = "ERROR - Your file was not uploaded 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.";
        }
    }

    private string ApplyXSLTransformation()
    {
        String html;

        String strXstFile = Server.MapPath("ncl.xslt");
        XslCompiledTransform x = new XslCompiledTransform();

        // Load the XML - use (Server.MapPath("/koalatemp/" + 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, Encoding.ASCII);
        StreamReader sr = new StreamReader(ms);
        xslt.Transform(doc, null, writer);
        ms.Position = 0;
        html = sr.ReadToEnd();
       //Response.Write(rd.ReadToEnd());
        sr.Close();
        ms.Close();
        //Server.Transfer(("C:/koalatemp/" + FileUpload1.FileName));
        return html;
       
    }
}
0

right I stopped it from writing
name.html.html

by removing red

//write HTML from stream to file
FileStream fs = new FileStream("C:/koalatemp/" + fileName+".html", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(result);
//result = Path.ChangeExtension(fileName, ".html");

0

Of course it will, read your code:

FileUpload1.SaveAs(savePath);

saves the file to : C:/temp/(savepath)

However, you change the extension to filename AFTER you assigned filename to savepath. So savepath never got updated lol.

Then you create a new file .html, and apply the style, then save that one lol.

flipflop these:
savePath += fileName;
fileName = Path.ChangeExtension(fileName,".html");

0

and you should probably add an if statement there to see if the file exists before opening it. Because if it opens a non existing file, it will just be blank anyway. Throw a if statement to catch the error.

0

lol by flip flop dya mean swap all instances of savepath with filename and vice versa? or some specific instances?>

0

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 :)

0

ok thanks. LOL now i am getting runtime error at line
Xpathdocument doc = new xpathdocument((C/temp+fileupload.filename))
error says it cant find "..........xml".

0

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

ApplyXSLTransformation(savepath)

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

:)

0

no clue. I'm not a C# programmer. Funny... I am a javascript programmer and I don't know C#?

They are basically the same ^^

Just throw the savepath in the transformation function as an argument, and trade out the "filename" with save path.

0

lol changed savepath to global var (well c# equivalent - static variable) and replaced all fileupload1.filename in ApplyXSLTransformation to savepath but runtime error of 'this class is not supported' at this line:
Xpathdocument doc = new xpathdocument((C/temp+fileupload.filename))

0

is it not fileName I need to replace fileupload1.filename with? becuase I cannot make fileName global (static)

0

ok. I think this is not going to work becuase the xml file is written, THEN transformed to an output of HTML. so I need to figure out a way of deleting all those excess xml files after processing. any ideas>

0

Great! What I have done to fix this is I added in a system.file.io.delete (savepath) after transform is complete. Now I have just the HTML file Brill. Thanks man for your help. Hope I can help you out someday!

0

You should just pass an argument to ApplyXSLTransformation that will set the current filename. Example:

ApplyXSLTransformation(savepath);

function ApplyXSLTransformation(string savepath)
...
...
end function

Within the ApplyXSLTransformation function, replace all "fileupload1.filename" with savepath.

If you look at it, you are creating a new file name with the .html end tag, then saving it.

Now in the function you are trying to grab the supplied filename, which still has the .xml, so it doesn't exist. This is why you get the error.

You need to pass the new file name to the function, which is "savepath". Figure out a way to pass that to the function in order to grab the correct file.

0

I am going to take this project another level now. I am going to use ASP.NET to call a program called ABEE (That creates .chm help files) to compile my HTMLs into one windows help file! good plan eh would that be tricky ?

0

lol fair enough. I cant imagine it will be easy to convert from HTML to HHP (Html Help Project) using ASP without calling third party software but maybe I am wrong.....we shall see lol .thanks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.