Ubhaya Kalanamiththa Ubhaya Kalanamiththa - 1 month ago 17
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>();
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?

Answer

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.