none
How to Delete XML File from Remote server at runtime

    Question

  •  Hi Team,

    I have simple project in Asp.net  reading XML file on remote server.

    I upload xml file using upload control , which will be saved on server database . for next file reading I want to delete previous uploaded file .

    ===================================

    I have used this code :

    protected void Button1_Click(object sender, EventArgs e)
        {

            if (FileUpload1.HasFile)
           {
                Label32.Visible = false;
                FileUpload1.SaveAs(Server.MapPath("~/") + FileUpload1.FileName);
            }
            if (FileUpload1.HasFile)
            {
                Panel1.Visible = true;
                Panel2.Visible = true;

                FileUpload1.Visible = false;
                Button1.Visible = false;
                XmlReader xmlRdr = XmlReader.Create(Server.MapPath("~/") + FileUpload1.FileName);
               

                while (xmlRdr.Read())
                {
                    if (xmlRdr.Name == "CompanyName")
                    {
                        Label13.Text = (Label13.Text == string.Empty) ? xmlRdr.ReadString() : Label13.Text;

                    }

    ======================================================

    Please tell me how to delete it ? ?

    -Many Thanks

    :omshree


    Saturday, November 16, 2013 12:01 PM

Answers

  • I think that you are in the Wrong forum.

    as I see it you want to cleanup after the upload and parsing is done.

    then after finished parsing and releasing the xml objects you'll need to do

    if (File.Exists(Server.MapPath("~/") + FileUpload1.FileName))
    {
    File.Delete(Server.MapPath("~/") + FileUpload1.FileName);
    }

    exists

    http://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx

    delete

    http://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

    Sunday, November 17, 2013 11:38 AM
  • Hi tetitu

    This is the same explanation as RSingh wrote in his first response, But it is not sure to be correct!

     read my response above :-) Using sql 2012 you can run this code and insert a upload a file directly to the SQL Server database. the database is like black box and the SQL Server can manage the data as it was on a regular folder. This is why i asked him for more information :-)

    * RSingh & you probably post the solution that fit to his problem, and he is not using the database probably (this is my guess too... but this is only Assumption). it is not sure to be the case! maybe he is using the database (SQL 2012 FILESTREAM) and his question fit to this forum :-) in any case the solution you post probably fit as RSingh's sulotion too.

    ** i just comment on the "Wrong forum" as i comment on "Your C# code does not upload data in the SQL Server" before :-)


    [Personal Site] [Blog] [Facebook]signature

    Monday, November 18, 2013 10:16 AM
    Moderator

All replies

  • Hi

    the XML File is inside an SQL Server?

    if so, what type of column do you use, and what version of SQL Server?

    * there are several type of column fit for storing files, and the type you can use are different from version to version.


    [Personal Site] [Blog] [Facebook]signature

    • Edited by pituachModerator Saturday, November 16, 2013 4:38 PM fixing type mostake: onside -> inside
    Saturday, November 16, 2013 12:43 PM
    Moderator
  • Your C# code does not upload data in the SQL Server. It is uploading in the web server folder.

    Check this link to know about how to upload XML file in SQL Server. http://social.msdn.microsoft.com/Forums/en-US/d1666d13-dea3-4ce8-a818-6b852a63de4f/store-xml-in-sql-server-2008-from-c?forum=vstsdb

    http://sqlservr.blog.com/2012/04/13/how-to-import-xml-file-into-sql-server/

    http://forums.asp.net/t/1779380.aspx

    In order to delete the existing XML file in SQL Server, we need an ID field to identity the row and delete the previous uploaded XML file.


    Regards, RSingh




    Saturday, November 16, 2013 4:19 PM
    Moderator
  • Not sure RSingh, 

    Using SQL Server 2012 you can work with file in Windows Explorer (and any application), like the file is on a folder :-) This is why i asked for the SQL Server version.

    More information can be read here: 
    https://www.simple-talk.com/sql/learn-sql-server/an-introduction-to-sql-server-filestream/
    Check fot the subtitle "Accessing FILESTREAM data with Managed API"


    [Personal Site] [Blog] [Facebook]signature

    Saturday, November 16, 2013 4:39 PM
    Moderator
  • Hi pituach, ASP.Net has the capability to upload a file, read the file (server side) and store its content to a SQL Server Table(appropriate data type). And even we can not track the upload history in the web server folder until and unless the upload history is recorded in the SQL Server table. :-)

    Regards, RSingh

    Saturday, November 16, 2013 6:06 PM
    Moderator
  • Sorry I don't get what you mean by that, You are just saying now, what i was saying before:

    Using SQL Server 2012 you can work with file in Windows Explorer (and any application), like the file is on a folder :-) This is why i asked for the SQL Server version."

    that is why your preview post was not sure to be true:
    Your C# code does not upload data in the SQL Server.
    What you wrote before is not true using SQL 2012As we can not know if it is upload to SQL Server or not until we get the column type in use and make sure what he is doing.

    *  Now you just said the same as i did before, so i don't get what you mean by telling it to me, while I wrote it for you :-)
    I am a developer and ASP.Net MVC is my main tech, by the way. Did't you read the interview with me :-) I was developing in 1983 and i work with SQL Server just from 2000.


    [Personal Site] [Blog] [Facebook]signature

    Saturday, November 16, 2013 8:10 PM
    Moderator
  • I think that you are in the Wrong forum.

    as I see it you want to cleanup after the upload and parsing is done.

    then after finished parsing and releasing the xml objects you'll need to do

    if (File.Exists(Server.MapPath("~/") + FileUpload1.FileName))
    {
    File.Delete(Server.MapPath("~/") + FileUpload1.FileName);
    }

    exists

    http://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx

    delete

    http://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

    Sunday, November 17, 2013 11:38 AM
  • Hi tetitu

    This is the same explanation as RSingh wrote in his first response, But it is not sure to be correct!

     read my response above :-) Using sql 2012 you can run this code and insert a upload a file directly to the SQL Server database. the database is like black box and the SQL Server can manage the data as it was on a regular folder. This is why i asked him for more information :-)

    * RSingh & you probably post the solution that fit to his problem, and he is not using the database probably (this is my guess too... but this is only Assumption). it is not sure to be the case! maybe he is using the database (SQL 2012 FILESTREAM) and his question fit to this forum :-) in any case the solution you post probably fit as RSingh's sulotion too.

    ** i just comment on the "Wrong forum" as i comment on "Your C# code does not upload data in the SQL Server" before :-)


    [Personal Site] [Blog] [Facebook]signature

    Monday, November 18, 2013 10:16 AM
    Moderator
  • I quote omshree

    "I have simple project in Asp.net  reading XML file"

    "I upload xml file using upload control "

    "I want to delete previous uploaded file "

    he is parsing the xml via asp.net and do insert into a table

    he does not save the xml directly on the SQL

    "FileUpload1.SaveAs(Server.MapPath("~/") + FileUpload1.FileName);"

    he wants to clean after him but the logic that he is proposing is wrong.

    he wants to delete the previous uploaded file.

    but what happens if the is a parallel of two uploads, or worse two uploads with the same filename.

    instead of saving the file he can stream the content of the FileUpload into the XmlReader object

    http://stackoverflow.com/questions/2679963/reading-the-xml-file-in-server-without-saving-it

    or he can use temporary file name.

     e.g.

    congratulation Ronen on the wiki interview

    http://blogs.technet.com/b/wikininjas/archive/2013/11/11/interview-with-ronen-ariely-many-times-technet-guru-competition-winner.aspx

    Monday, November 18, 2013 11:52 AM
  • using asp.net we can store files on the database using c# classes. The developer dont even have to know that the folder is not a real folder but a place on the database:-) read the link i gave above for more info. as you can with asp.net like this is a regular folder i mention that your solution will work :-) i just wanted to point that it might be for this forum (probably not).

    * from SQL 2012

    Thanks for the congratulate :-)


    [Personal Site] [Blog] [Facebook]signature

    Monday, November 18, 2013 1:08 PM
    Moderator
  • Hi omshree,

    Is this still an issue ?


    Regards, RSingh

    Tuesday, November 19, 2013 2:12 AM
    Moderator