在Asp.net中生成XML

时间:2016-11-03 12:49:00

标签: c# asp.net xml

我试图在代码中生成动态xml文件并下载它。这是我习惯的功能

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);

此功能正常运行。但是当下载的xml文件包含相关页面的html代码时。

<?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>

因为我无法使用xml解析解析此文件。它给了我这个错误。

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.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码。我之前使用它来进行我需要做的导出,只需根据您的需要进行调整:

 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();

XElement类有一个能够解决问题的保存功能。

否则正如@Mason所说,你需要在你的代码中添加response.end()部分。如果正确完成,HTML将不再存在。