Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have tried and downloaded using ".xlsx". It will be downloaded in new tab and asks to user where to save the particular file.

But when i have tried same for xml download, it does not happen.

Here is the code for .xlsx
fileName = downloadFileName + "_" + currentTime + ".xlsx";
     System.Web.HttpContext.Current.Response.Clear();
     System.Web.HttpContext.Current.Response.ClearContent();
     System.Web.HttpContext.Current.Response.ClearHeaders();
     System.Web.HttpContext.Current.Response.Buffer = true;
     System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                                                                                                                                                                                                                                    System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
      System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
      System.Web.HttpContext.Current.Response.BinaryWrite(xlPackage.GetAsByteArray());
      System.Web.HttpContext.Current.Response.End();
      System.Web.HttpContext.Current.Response.SuppressContent = true;


This code works fine when downloading for xlsx. Below code is used to download for Xml, Which i could able to do.

 string downloadFileName = "Template_Xml";
                    string currentTime = DateTime.Now.ToString("ddMMyyyyhhmmss");
                    fileName = downloadFileName + "_" + currentTime + ".xml";
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.ClearContent();
                    System.Web.HttpContext.Current.Response.ClearHeaders();
                    System.Web.HttpContext.Current.Response.Buffer = true;
                    System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    System.Web.HttpContext.Current.Response.ContentType = "text/xml";
                    System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);

// Xmldocument creation

  XmlDocument docConfig = new XmlDocument();
  XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
  XmlElement rootElement = docConfig.CreateElement("User");
  docConfig.AppendChild(rootElement);
  XmlElement environmentElement = docConfig.CreateElement("NAME");
  XmlText environText = docConfig.CreateTextNode("ABC" + i);
  environmentElement.AppendChild(environText);
  hedder.PrependChild(environmentElement);

  System.Web.HttpContext.Current.Response.BinaryWrite(docConfig); // It throws a error
  System.Web.HttpContext.Current.Response.End();


What I have tried:

Here is the code for downloading XML,

<pre> string downloadFileName = "Template_Xml";
                    string currentTime = DateTime.Now.ToString("ddMMyyyyhhmmss");
                    fileName = downloadFileName + "_" + currentTime + ".xml";
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.ClearContent();
                    System.Web.HttpContext.Current.Response.ClearHeaders();
                    System.Web.HttpContext.Current.Response.Buffer = true;
                    System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    System.Web.HttpContext.Current.Response.ContentType = "text/xml";
                    System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);

// Xmldocument creation

  XmlDocument docConfig = new XmlDocument();
  XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
  XmlElement rootElement = docConfig.CreateElement("User");
  docConfig.AppendChild(rootElement);
  XmlElement environmentElement = docConfig.CreateElement("NAME");
  XmlText environText = docConfig.CreateTextNode("ABC" + i);
  environmentElement.AppendChild(environText);
  hedder.PrependChild(environmentElement);

  System.Web.HttpContext.Current.Response.BinaryWrite(docConfig); // It throws a error
  System.Web.HttpContext.Current.Response.End();
Posted
Comments
F-ES Sitecore 20-Jun-17 10:08am    
What is the error?
vinodh muthusamy 21-Jun-17 0:51am    
Error 5 The best overloaded method match for 'System.Web.HttpResponse.BinaryWrite(byte[])' has some invalid arguments

Error 6 Argument 1: cannot convert from 'System.Xml.XmlDocument' to 'byte[]'

These 2 errors iam getting

F-ES Sitecore 21-Jun-17 4:33am    
Look at the documentation for BinaryWrite and you'll see it only accepts an array of bytes as a parameter (byte[]) and you are passing an XmlDocument. It doesn't know how to turn an XmlDocument into an array of bytes so you'll need to do that bit yourself. Google "XmlDocument to byte array"

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/7cdd4bf7-3da3-4735-b710-833ff3ffe693/convert-xml-data-to-byte-array?forum=csharpgeneral

So it'll be something like


System.Web.HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(docConfig.OuterXml));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900