Click here to Skip to main content
13,345,744 members (49,886 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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.

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

        Response.ContentType = "application/xml"; //or text/xml

        Response.AddHeader("content-disposition", String.Format("attachment;filename=" + Filename));

Posted 24-Jul-12 14:10pm
Updated 24-Jul-12 14:42pm
Wes Aday104.2K
Sergey Alexandrovich Kryukov 24-Jul-12 20:07pm
You are not trying to write a file, so how can it be wrong? :-)
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Look at the following:[^]

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

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[^] for more information on OpenFileDialog, and[^] for SaveDialog
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

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();
string strXML = GetXMLString(doc);
HttpResponse response = HttpContext.Current.Response;
response.Buffer = true;
response.AddHeader("ContentType", "application/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=\"FileName.xml\";");
Clifford Nelson 25-Jul-12 13:57pm
You must not be running in Desktop. Are you running ASP.NET or Silverlight.. I have seen issues with STA (single thread apartment) but not this. Look at I suspect you will have to do some custom dialogs. I am not an ASP.NET developer. THis may also help:

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100