1

In a .Net web-service, when you click "invoke" button an XML file is opened.

I have a page with a button. I want when I click it an XML page be opened like web-service pages. I have written the following code but it does not return an XML page. how can i do that?

1) I am in "http://mysite/default.aspx"

2) click my button

3) This page be opened with the .Xml extension: "http://mysite/result.xml"

 Response.ClearHeaders();
 //Response.AppendHeader("content-disposition", "attachment;filename=result.xml");
 Response.AddHeader("content-type", "text/xml");
 Response.Write("aaaa");
 Response.End();

2 Answers 2

0

It write the code Response.Redirect("xmlfile.xml"); and it is showing xml as you expected.

0
0

You need to add a contenttype to your response.

Response.Clear();
Response.ContentType = "text/xml";          
Response.Write("<?xml version=\"1.0\">");
//whatever you want
Response.End();

PS.content-disposition will give you an "option" to download the file (the browser will download the file, not posting it to the browser itself);

Response.ContentType = "application/" + System.IO.Path.GetExtension(pathToFile).Substring(1).ToLower();
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(pathToFile)));
Response.WriteFile(pathToFile);
Response.End();
2
  • your sample code does not open a new window containing my file contents.
    – Bob
    Apr 7, 2012 at 6:17
  • @breceivemail ofcourse it doesn't. Openening a new window for download is the second codesample. But opening an XML file in a new window is up to you (add a target to your anchor for instance).
    – riffnl
    Apr 11, 2012 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.