Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can someone please tell me how to save an xml file from a site Url to local system by prompting through Save Dialog box.

I have written the following code rather writing the whole content on browser window itself rather than prompting through dialog box. Please tell me where I doing wrong.

C#
string Filename = "http://localhost/SiteName/FileName.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(Filename);
        string strXML = GetXMLString(doc);//Converts XMLDocument to string

        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        Response.ContentType = "application/xml"; //or text/xml

        Response.AddHeader("content-disposition", String.Format("attachment;filename=" + Filename));
        Response.WriteFile(strXML);     
        Response.Flush();
        Response.Close();
        Response.End();

Thanks.
Posted
Updated 24-Jul-12 13:42pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Jul-12 20:07pm    
You are not trying to write a file, so how can it be wrong? :-)
--SA
lukeer 25-Jul-12 2:47am    
Looks like you are working on some web-based stuff. The user's browser should then be able to save a file. It does so by showing the user a dialog box he is familiar with. You would only have to provide a link.

1 solution

Look at the following: http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx[^]

Here is the basic code for an open dialog for WinForms (WPF would be slightly different):

C#
var save = new SaveFileDialog();
var open = new OpenFileDialog();
var result = open.ShowDialog();
if (result == DialogResult.OK)
    MessageBox.Show("Filename selected to open is " + open.FileName);



See http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx[^] for more information on OpenFileDialog, and http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx[^] for SaveDialog
 
Share this answer
 
v2
Comments
InfySam 24-Jul-12 21:16pm    
Pardon me if I am not clear regarding my requirement. I want to display a Save/Open/Cancel dialog box inorder to save the xml file(This xml file is from a site) on local system.
Clifford Nelson 24-Jul-12 22:56pm    
Must be that you want to save a file on a remote system. Am I correct. Otherwise it is just using the Open/Save dialogs. If you do want to save/open to a remote system, you have to have installed program on the remote system because Windows does not like to allow remote access to a oomputer for obvious reasons. One way to do this is work thru the PowerShell remote features.
InfySam 25-Jul-12 8:47am    
Hi all, Thanks for your replies. My apologies because this seemed I did not properly state my requirement (my bad :().
All that I want is on click of a button I want a save/open dialogbox to appear.
Can someone please help me with this?
Clifford Nelson 25-Jul-12 12:46pm    
I have added to my response. Hope that helps.
InfySam 25-Jul-12 13:35pm    
Hi,

Adding above code in my button click event gives the following error,


Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

I am still unable to resolve this problem.
But I prefer open/Save download dialog box to the SaveFileDialog(), OpenFileDialog() and I think this can be achieved by using Content-Disposition. However using the following code is displaying content in the browser rather than popping up Open/Save/Cancel download dialog box.

string Filename = "http://localhost/SiteName/FileName.xml";
XmlDocument doc = new XmlDocument();
doc.Load(Filename);
string strXML = GetXMLString(doc);
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("ContentType", "application/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=\"FileName.xml\";");
response.Write(strXML);
response.End();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900