I am writing XML data to the user browser for download. Below is the code
protected void WriteXmlToPageDownload(XElement XmlCustomerData) {
try
{
if (XmlCustomerData != null)
{
XDocument x = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement(XmlCustomerData));
StringBuilder builder = new StringBuilder();
StringWriter writer = new StringWriter(builder);
x.Save(writer);
writer.Flush();
HttpContext context = HttpContext.Current;
context.Response.Write(builder.ToString().Replace("encoding=\"utf-16\"", "encoding=\"utf-8\""));
context.Response.ContentType = "application/xml";
string filename = "CustomerDetails ";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".xml");
context.Response.AppendHeader("Connection", "close");
context.Response.End();
}
}
catch (Exception ex)
{
Log("Exception in CustomerData" + ex.Message.ToString(), this);
}
}
The XmlCustomerData contains the XML created by XElement(some business logic...) Below is sample of the XML required
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Customers>
<Customer>
<deatils name="ABC">
<id="1">
</details>
<deatils name="XYZ">
<id="2">
</details>
...
</Customer>
</Customers>
and the XmlCustomerData contains the business logic to hold complete
<Customers>...</Customers>
The issues is. 1.XML is got getting created and also not getting downloaded to the user browser 2.And i am getting the exception "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
My requirement is to download the XML file to the user's browser . Please let me know what mistake i am doing or a better way of it
Thanks in advance.