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
LVL 2
firepolAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Solar_FlareCommented:
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.
0
firepolAuthor Commented:
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
0
firepolAuthor Commented:
I solved my problems by

1) saving the file (instead of using a direct output streaming)

2) onButton_click {
//I did some processing
//wrote the file

//at the end, redirect to the generated file
Response.Redirect("GeneratedFile.xml");
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.