0

I'm trying to generate dynamic xml file in the code and download it. this is the function i used to it

xmlUrl Url = new xmlUrl();
Url.Url = fileName.Text;
List<xmlUrl> Urls = new List<xmlUrl>();
Urls.Add(Url);
XmlSerializer ser = new XmlSerializer(Urls.GetType());
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
ser.Serialize(writer, Urls);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());


doc.WriteTo(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();

byte[] byteArray = stream.ToArray();

Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + lblFile.Text + ".xml");
Response.AppendHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "text/xml";
Response.BinaryWrite(byteArray);

this function is working. but when the xml file downloaded it contain the html code of the relevent page.

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfXmlUrl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xmlUrl>
<Url>
Url of the web content
</Url>
</xmlUrl>
</ArrayOfXmlUrl>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
source of the body
</body>

there for i cannot parse this file with xml parse. its giving me this error.

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

Additional information: There is no Unicode byte order mark. Cannot switch to Unicode.

how do i fix this?

  • 1
    Try to search for error message, there are plenty of hits, e.g. [here](http://stackoverflow.com/q/29915467/1997232). – Sinatr Nov 03 '16 at 12:50
  • Once you write the file to the response, you need to end it so that page execution ends. If those is going to cause a problem because you needed to manipulate the page, then you should have the PDF download occur in a separate browser window. – mason Nov 03 '16 at 12:52
  • I need to remove the html part in the XML. its junk for the xml. how do i do this? – Ubhaya Kalanamiththa Nov 03 '16 at 12:53

1 Answers1

1

You can try the following code. I used it previously for a export I needed to do, just adapt it for your needs:

 XElement XmlExp = new XElement("StockItems",
                                        from stock in DAL.GetEquipmentStockSummary()
                                        select new XElement("Item",
                                                             new XAttribute("ID", stock.EquipmentStockID),
                                                             new XElement("Tag", stock.TagNum),
                                                             new XElement("Model", stock.SubGroup),
                                                             new XElement("Desc", stock.Description),
                                                             new XElement("Material", stock.Finish),
                                                             new XElement("Condition", stock.isUsed ? "Refurbished" : "New"),
                                                             new XElement("Location", stock.LocationName)
                                                             )
                                      );

        context.Response.Headers.Add("Content-Disposition", "attachment;filename='SOHList.xml'");
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;

        XmlExp.Save(context.Response.Output);
        context.Response.End();

The XElement class has a save function that will be able to solve your issue.

Otherwise as @Mason said, you need to add the response.end() part to your code. If done correctly the HTML will no longer be there.

ThatChris
  • 752
  • 1
  • 4
  • 18