简体   繁体   中英

How can I convert a uploaded XML file that is in the form of a HttpPostedFIle to a string in C# asp.net?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM