How to create an XML file with ASP.NET and ask to download it?

I am currently trying to write data (client machine) to an xml file where the user can save. However, I want users to be able to decide where they want to save this written XML file. Are there any controls or codes that I can use to allow users to save the file ?.

Update:

Is this the right way to do it?

**HttpContext.Current.Response.Write(xw.ToStroing()); <<< ??????**
HttpContext.Current.Response.End(); 

update:

XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            MemoryStream ms = new MemoryStream();

HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "text/xml";
            HttpContext.Current.Response.AddHeader("Content-Disposition:", "attachment;filename=" + HttpUtility.UrlEncode(fileName));        

            using (StringWriter sw = new StringWriter())
            {
                using (XmlWriter xw = XmlWriter.Create(ms, settings))

                {
                    xw.WriteStartDocument();

                    xw.WriteStartElement("Name");
                    xw.WriteStartElement("Application");
                     ................
                    ......................
                    HttpContext.Current.Response.Write(xw.ToStroing());
                      HttpContext.Current.Response.End(); 
+5
source share
4 answers

here is the link helps me :)

+1
source

When you start downloading the file, the client browser will process the file’s save location.

, HTTP- :

Content-Disposition: attachment; filename="myFile.xml"
+1

Where is it used MemoryStream? You must pass StringWriterto the constructor XmlTextWriter, then the Response.Write()contentsStringWriter

0
source

Add this after the line Response.AddHeader:

Response.ContentType = "application/octet-stream";

This will force you to download files, regardless of file type.

0
source

All Articles