-1

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.

Troy
  • 11
  • 3

1 Answers1

1

Once you have loaded the xml data into the XDocument then the code

StringBuilder builder = new StringBuilder();
StringWriter writer = new StringWriter(builder);

Can be replaced with

x.Save(context.Response.OutputStream);

A quick mock-up of similar code, the code below will download the xml content after the user is asked to save or open the attachment. The code is contained within a Http Handler ashx file.

/// <summary>
/// Summary description for Handler1
/// </summary>
public class Handler1 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string xml =
            @"<?xml version=""1.0""?>
            <Customers>
                <Customer>
                    <details>
                        <id/>
                    </details>
                    <details>
                        <id/>
                    </details>
                </Customer>
            </Customers>";

        XElement xel = XElement.Parse(xml);
        XDocument x = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement(xel));

        context.Response.ContentType = "application/xml";
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=Test.xml");
        context.Response.AddHeader("Content-Length", xml.Length.ToString());
        x.Save(context.Response.OutputStream);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

This code displays the xml directly on the web page. The code is contained within a Http Handler ashx file.

/// <summary>
/// Summary description for Handler2
/// </summary>
public class Handler2 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string xml =
            @"<?xml version=""1.0""?>
            <Customers>
                <Customer>
                    <details>
                        <id/>
                    </details>
                    <details>
                        <id/>
                    </details>
                </Customer>
            </Customers>";

        XElement xel = XElement.Parse(xml);
        XDocument x = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement(xel));

        context.Response.ContentType = "application/xml";
        context.Response.AddHeader("Content-Length", xml.Length.ToString());
        x.Save(context.Response.OutputStream);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
D.Zadravec
  • 647
  • 3
  • 7