Ubhaya Kalanamiththa Ubhaya Kalanamiththa - 7 months ago 69
ASP.NET (C#) Question

Generating XML in Asp.net

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>();
XmlSerializer ser = new XmlSerializer(Urls.GetType());
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
ser.Serialize(writer, Urls);
XmlDocument doc = new XmlDocument();


byte[] byteArray = stream.ToArray();

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

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">
Url of the web content

<!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" />
source of the 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?


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;


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.