[英]Generating XML in Asp.net
I'm trying to generate dynamic xml file in the code and download it. 我正在尝试在代码中生成动态xml文件并下载它。 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文件时,它包含相关事件页面的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>
there for i cannot parse this file with xml parse. 那里因为我无法使用xml解析来解析此文件。 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;
XmlExp.Save(context.Response.Output);
context.Response.End();
The XElement class has a save function that will be able to solve your issue. XElement类具有保存功能,可以解决您的问题。
Otherwise as @Mason said, you need to add the response.end() part to your code. 否则,如@Mason所说,您需要在代码中添加response.end()部分。 If done correctly the HTML will no longer be there. 如果正确完成,HTML将不再存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.