0

How can I convert a uploaded XML file that is in the form of a HttpPostedFIle to a string in C# asp.net? I am trying to create the ability to upload an XML file and store it on a database server for a client that is using my web application. I need to convert it to a string so I can do string manipulation to the XML file and remove some elements in the XML file that are not compatible with SQL server.

I've tried this and a few other things. I keep getting the error

InputStream is not in this current context

and another error.

string xmlString = System.IO.File.ReadAllText(fl.InputStream.ToString());

            string fName = fl.FileName;
            if (fName.IndexOf("\\") != -1) { fName = fName.Substring(fName.LastIndexOf("\\") + 1); }
            string fileDataLink = uniqueName + Path.GetExtension(fName);
            outputPath += fileDataLink;
            fl.SaveAs(outputPath);
            transactionTypeID = Convert.ToInt32(Request["textInput"]);
            integrationTypeDt = DAC.ExecuteDataSetText(db.ConnectionString, "SELECT [StoredProcName], [Configuration] FROM [dbo].[ITransactionType] WITH(NOLOCK) WHERE [TransactionTypeID] =" + transactionTypeID, new string[] { "IntegrationType" }).Tables[0];

            string workbookXML = "";
            //load the file,
            string xmlString = System.IO.File.ReadAllText(fl.InputStream.ToString());

            //make changes

            //Assign the file to the XML var, 


            //and then save it into the database

I expect it to return a string value of the entire file that was uploaded. Instead, I am getting two errors. One says InputStream is not in the current context.

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
Jeffrey Padgett
  • 218
  • 1
  • 14
  • Does the line `fl.SaveAs(outputPath);` work and save the file to disk? – mortb Apr 03 '19 at 07:53
  • Yes, do.SaveAs(outputPath) does work. – Jeffrey Padgett Apr 03 '19 at 07:54
  • `ReadAllText("Your exact file path")` what is the value of this? `fl.InputStream.ToString()` – Vijunav Vastivch Apr 03 '19 at 07:55
  • 1
    It might be that the `SaveAs(...)` has consumed the stream so that it might not be read again, you can try to remove that line and see if it works. Here is a link to an example of using fileupload https://www.aspsnippets.com/Articles/Upload-and-Download-PDF-file-Database-in-ASPNet-using-C-and-VBNet.aspx – mortb Apr 03 '19 at 08:00
  • When I run it in debug mode. I get two errors. Cannot convert method group 'ToString" to non-delegate type 'object. Did you intend to invoke the method? And another error that states The name 'InputStream' does not exist in the current context. When I try to view InputStream, it has a length of 983 and a read time out that states System.InvalidOperationException – Jeffrey Padgett Apr 03 '19 at 08:03
  • Oh, I didn't see that... The statement `System.IO.File.ReadAllText(fl.InputStream.ToString())` is all wrong :) The parameter to `ReadAllText` is supposed to be a file path to a disk file, not the stream. You'd need to use `StreamReader` to get the contents of the stream as a string – mortb Apr 03 '19 at 08:10
  • The reason that it is gray is that it is an unused variable; it's value is not used in the code below. I guess that is because your code still is work in progress? – mortb Apr 03 '19 at 08:12
  • Thank you mortb, could you give me an example of how I could apply that? could you write that out as an answer for me? – Jeffrey Padgett Apr 03 '19 at 08:17
  • It was gray because there is a simpliar way to write it. I did not need to include the System.IO for conventional reasons. But I think you are on to something with the StreamReader. Could you give me an example of how to apply it to that variable xmlString? – Jeffrey Padgett Apr 03 '19 at 08:18
  • Give me a couple of minutes... – mortb Apr 03 '19 at 08:22

1 Answers1

1

There are several ways to fix your code. This would be one way:

        string fileDataLink = uniqueName + Path.GetExtension(fl.FileName);
        outputPath += fileDataLink;
        // not needed if you do not want the file on both disk and database 
        //fl.SaveAs(outputPath);

        transactionTypeID = Convert.ToInt32(Request["textInput"]);
        integrationTypeDt = DAC.ExecuteDataSetText(db.ConnectionString, "SELECT [StoredProcName], [Configuration] FROM [dbo].[ITransactionType] WITH(NOLOCK) WHERE [TransactionTypeID] =" + transactionTypeID, new string[] { "IntegrationType" }).Tables[0];

        string workbookXML = "";
        //load the file,
        string xmlString = null;
        using(var reader = new StreamReader(fl.InputStream))
        {
           xmlString = reader.ReadToEnd();
        }

Another way would be to keep the SaveAs and write:

string xmlString = System.IO.File.ReadAllText(outputPath);

Which would load the file from disk.

Please note two things:

  1. Your solution loads the entire xml file into memory for processing. This might not be a problem in your scenario, but if you for example have many users and they upload multiple large files simultaneously your server might run out of memory. In such a case it would be better to try a different solution that in some way feeds the stream as a stream to the database.
  2. You should never concatenate a value that is input from a web page to your sql as is done in the statement "SELECT [StoredProcName], [Configuration] FROM [dbo].[ITransactionType] WITH(NOLOCK) WHERE [TransactionTypeID] =" + transactionTypeID Use SQL parameters instead instead.
mortb
  • 9,361
  • 3
  • 26
  • 44