tags:

views:

295

answers:

5

I have created a file with

XmlDocument xmldoc = new XmlDocument();

Can I make this file as downloadable? without saving it?

A: 

You'll want to look at writing a Custom HTTP Handler (a class that implements IHttpHandler) and simply register it in web.config. See this article on MSDN for a good example of how to set one up.

Here's a basic example of how you might go about implementing one to return the markup for an XmlDocument.

using System.Web;

public class MyXmlDocumentHandler : IHttpHandler
{
    public static XmlDocument XmlDoc
    {
        get;
        set;
    }

    public MyXmlDocumentHandler()
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/xml"; // Set the MIME type.
        XmlDoc.WriteTo(context.Response.OutputStream); // Write the XML markup to the respone stream.
    }

    public bool IsReusable
    {
        // To enable pooling, return true here.
        // This keeps the handler in memory.
        get { return false; }
    }
}
Noldorin
+1  A: 

This is how:

xmldoc.Save(Response.OutputStream)

Don't forget to set response mime type and other relevant properties so client browser will understand it as a file download.

Robert Koritnik
+1  A: 

You could do something like:

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<xml>myxml</xml>");
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=MyXmlDocument.xml");
Response.AddHeader("Content-Length", doc.OuterXml.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Write(doc.OuterXml);
Kev
This is the simplest one.
Vikas
A: 

Differently from what Noldorin posted you don't have to create a Custom HTTP Handler.

If you want to download the XML document created by the XmlDocument class, you can always save it directly to the stream, setting its content type to text/xml.

More or less like the following code:

XmlDocument xmldoc = new XmlDocument();

/*
 * ... more code ...
 */

this.Response.ContentType = "text/xml";

xmldoc.Save(this.Response.OutputStream);
Paulo Santos
You don't have to register a custom handler, but I've found it easiest to create a .ashx file (+code-behind) which writes the appropriate XML. I prefer that to using a normal page which one might expect to create HTML.
Jon Skeet
A: 

You'll need to respond to a request for it, saving the document to the response. Of course, you'll need to be able to get the XmlDocument somehow - if you've created it in one request (e.g. for a page) you'll need to either be able to recreate it based on parameters, of cache it in the session (with all the normal caveats).

For an example of writing it out, within a normal Page you might do:

xmldoc.Save(Response.OutputStream);

or

xmldoc.Save(Response.Output);

You can easily create a .ashx file and associated code-behind (new "Generic Handler" item) and then in the code-behind which implements IHttpHandler implement ProcessRequest with:

public void ProcessRequest(HttpContext context)
{
    XmlDocument doc = ...;

    doc.Save(context.Response.OutputStream);
}

You may also want to set an appropriate content type (probably "text/xml" unless it's a specific XML format which you'd want to be interpreted differently) etc. If you want the client to default to saving it, you should set a content disposition.

Jon Skeet