Link to home
Start Free TrialLog in
Avatar of firepol
firepol

asked on

c# on(button)click: download xml that I get as XmlDocument from a method

I've created an ajax website with Visual Studio 2005, the aspx page has scriptmanager and an updatepanel. Inside the updatepanel I have a gridview, an objectdatasource and a button called "Download XML". The page loads the gridview correctly, I have problems with the download...

When I select a row and I click "Download XML" I call a method which gives me as output an XmlDocument:

XmlDocument xml = myMethod(myselectedrow);

What I tried to do now is to get the file, to download. I tried the following ways (I googled around and tested also other ways...) but of course they don't work:

Way 1:

        Response.ContentType = "application/x-unknown"; //application/x-unknown   text/xml
        xml.Save(Response.OutputStream);

Resulting popup error from internet explorer:

"
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near '<?xml version="1.0"?'.
"

If I use "text/xml" as contenttype, same error.

I've googled around and found out this page: http://weblogs.asp.net/leftslipper/archive/2007/02.aspx

I tried, as suggested at the end of the article, to put the Download button outside the updatepanel, but I get this error when I press it:

"
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

Cannot have a DOCTYPE declaration outside of a prolog. Error processing resource 'http://localhost/myproject

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&...


Way 2 I tried:

        string xml = xmlExportedPPRs.ToString();
        Response.ContentType = "text/xml";
        byte[] debugFile = System.Text.Encoding.Default.GetBytes(xml);
        Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", "debug.xml"));
        Response.BinaryWrite(debugFile);
        Response.End();

Result error, same as the 1st listed one, just the last line of the pupup is different:

"Details: Error parsing near 'System.Xml.XmlDataDo'.   "

I don't know what to do, I'm a beginner, I couldn't imagine that download a file from an object (XmlDocument) could be so difficult. Please help.

Cheers,

Paolo
Avatar of Solar_Flare
Solar_Flare

you cant do this with an ajax call. AJAX wants to update the existing page with the results of the async call, so you cant simply write text to the response.

You will need to either take the button outside of the updatepanel, or when the button is clicked insert a bit of script that opens a popup window to a URL that returns your XML.
Avatar of firepol

ASKER

I've already tried to put the button outside the updatepanel, what I get is this error:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Cannot have a DOCTYPE declaration outside of a prolog. Error processing resource 'http://localhost/Siemens.Libs.CAB.ILH-RCM...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&...
 


Then I need to create another aspx page to be called as popup when the button is clicked? I can imagine that then I need to put the XmlDocument into a Session to be used by the other aspx page...

Isn't there a way to make it work directly into my page? If yes, some code is welcome (I'm a beginner, I need some example code...)

Thannks again,

Paolo
ASKER CERTIFIED SOLUTION
Avatar of firepol
firepol

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial