【问题标题】:Generating XML in Asp.net在 Asp.net 中生成 XML
【发布时间】:2017-03-17 01:32:23
【问题描述】:

我正在尝试在代码中生成动态 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.

我该如何解决这个问题?

【问题讨论】:

  • 尝试搜索错误信息,有很多命中,例如here.
  • 将文件写入响应后,您需要结束它以便页面执行结束。如果由于您需要操作页面而导致出现问题,那么您应该在单独的浏览器窗口中下载 PDF。
  • 我需要删除 XML 中的 html 部分。它的 xml 垃圾。我该怎么做?

标签: c# asp.net xml


【解决方案1】:

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

 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 将不再存在。

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 1970-01-01
    • 2013-04-27
    • 2021-11-22
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2013-07-25
    相关资源
    最近更新 更多