[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2008-08-28
3
Medium Priority
?
1,741 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:firepol
  • 2
3 Comments
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 22339660
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
 
LVL 2

Author Comment

by:firepol
ID: 22343383
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
 
LVL 2

Accepted Solution

by:
firepol earned 0 total points
ID: 22365184
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question