Why doesn't IE recognize my output as XML?
I built the following code to output XML:
public static XDocument Serialize<T>(this T source) where T : class
{
XDocument document = new XDocument();
XmlReflectionImporter xmlReflection = new XmlReflectionImporter();
XmlTypeMapping xmlMapping = xmlReflection.ImportTypeMapping(typeof(T));
XmlSerializer xmlSeri开发者_StackOverflow中文版alizer = new XmlSerializer(xmlMapping);
using (XmlWriter xmlWriter = document.CreateWriter())
xmlSerializer.Serialize(xmlWriter, source);
return document;
}
Then in one of my aspx pages I have the following output:
XDocument output = GetSomeXmlSerializedOutput();
output.Save(Response.OutputStream);
GetSomeXmlSerializedOutput()
is basically output from feeding a class to the Serialize extension method.
The page's header looks like this:
<%@ Page Language="C#" CodeBehind="Alerts.aspx.cs" Inherits="Infinix.Diageo.WebApp.Get.Alerts" ContentType="text/xml" %>
Firefox correctly assumes from just the ContentType that the output is XML. IE doesn't. The output XML, for reference, is:
<?xml version="1.0" encoding="utf-8"?>
<ALERTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ALERT>
<ID>1</ID>
<TYPE>ALERT</TYPE>
<NAME>neim</NAME>
<DETAIL>diteil</DETAIL>
<DATE>11/28/2010</DATE>
<TIME>13:50:02</TIME>
</ALERT>
<ALERT>
<ID>2</ID>
<TYPE>EVENT</TYPE>
<NAME>iven</NAME>
<DETAIL>ditel</DETAIL>
<DATE>11/28/2010</DATE>
<TIME>13:50:15</TIME>
</ALERT>
<ALERT>
<ID>3</ID>
<TYPE>BIRTHDAY</TYPE>
<NAME>pijazo</NAME>
<DETAIL>grande!</DETAIL>
<DATE>11/28/2010</DATE>
<TIME>13:50:23</TIME>
</ALERT>
</ALERTS>
Why doesn't IE recognize this output as genuine XML?
Your page directive needs to set the ContentType="application/xml"
.
<%@ Page Language="C#" CodeBehind="Alerts.aspx.cs"
Inherits="Infinix.Diageo.WebApp.Get.Alerts" ContentType="application/xml" %>
精彩评论