XML not sent to client through response object correctly in IE

I have a download control that calls a handler from the client and sends the xml string back to the client. The code below works fine in chrome, but sends weird xml back to the client in IE.

Handler Code:

public void ProcessRequest(HttpContext context) { if (context.Request.Files.Count > 0) { var uploadInfo = context.Request.Files[0]; var uploadStream = uploadInfo.InputStream; var upload = new byte[uploadInfo.ContentLength]; uploadStream.Read(upload, 0, uploadInfo.ContentLength); var payload = System.Text.Encoding.UTF8.GetString(upload); XmlDocument doc = new XmlDocument(); doc.LoadXml(payload); context.Response.ContentType = "application/xml"; context.Response.Write(doc.OuterXml); //context.Response.Write(payload); } } 

An XML string is debugged from the handler to being sent to the client (which looks right)

 <?xml version="1.0"?> <ApplicationData> <DataElement name="Comments" type="String"></DataElement> <DataElement name="ModAuthor" type="String"></DataElement> <DataElement name="CreateDate" type="Datetime"></DataElement> <DataElement name="Author" type="String"></DataElement> <DataElement name="StatusDate" type="Datetime"></DataElement> <DataElement name="StatusDisp" type="String"></DataElement> <DataElement name="Status" type="Number"></DataElement> <DataElement name="Approved" type="Number"></DataElement> <DataElement name="Submitted" type="Number"></DataElement> <DataElement name="LogNumber" type="Number"></DataElement> </ApplicationData> 

Then an XML string that is sent back:

 <DIV class=e><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;?</SPAN><SPAN class=pi>xml version="1.0" </SPAN><SPAN class=m>?&gt;</SPAN> </DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em" class=c><A class=b onfocus=h() onclick="return false" href="#">-</A> <SPAN class=m>&lt;</SPAN><SPAN class=t>ApplicationData</SPAN><SPAN class=m>&gt;</SPAN></DIV> <DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>Comments</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>String</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>ModAuthor</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>String</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>CreateDate</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>Datetime</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>Author</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>String</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>StatusDate</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>Datetime</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>StatusDisp</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>String</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>Status</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>Number</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>Approved</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>Number</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV class=e> <DIV style="TEXT-INDENT: -2em; MARGIN-LEFT: 1em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>DataElement</SPAN> <SPAN class=t>name</SPAN><SPAN class=m>="</SPAN><B>Submitted</B><SPAN class=m>"</SPAN><SPAN class=t> type</SPAN><SPAN class=m>="</SPAN><B>Number</B><SPAN class=m>"</SPAN><SPAN class=m> /&gt;</SPAN> </DIV></DIV> <DIV><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;/</SPAN><SPAN class=t>ApplicationData</SPAN><SPAN class=m>&gt;</SPAN></DIV></DIV></DIV> 

For some reason, it is being translated from XML to the client in a completely different format.

+6
source share
3 answers

I have a strange feeling that this is due to the encoding, I had a similar problem with IE.

I see in your code that you are using UTF-8 encoding to get xml as a string. Try adding the following heading:

 <?xml version="1.0" encoding="UTF-8"?> 

into your xml file. I know that if you leave this, it will be automatically detected, but try to do this, and also try to open the file with something like Notepad ++ and save it in UTF-8 encoding. Also, if the file is generated pragmatically, check the code and try to force the UTF-8 encoding.

In my experience with web applications, the place where IE is screwed (most often) has some default values ​​other than Chrome, Firefox, Opera, Safari, etc. If you do not explicitly enter these values, the browser will set the default values, but if you enter them into the code, you will force a certain behavior. This is not required for Chrome, Mozilla, etc., but will "fix" the interpretation of IE.

One of the aforementioned defaults is encoding http://social.technet.microsoft.com/Forums/en-US/winserverGP/thread/2f5cdeff-3723-43bf-a880-d4a41222e24d/

+2
source

The translated XML looks like a formatted XML representation that IE generates when directly loading an XML file without an XSL stylesheet. I assume your load control is targeting a hidden iframe and using the DOM to load the response.

Depending on what you are doing with XML, you may slip away from loading it in plain text:

 context.Response.ContentType = "text/plain"; context.Response.AppendHeader("X-Content-Type-Options", "nosniff"); 

You need to add the X-Content-Type-Options: nosniff to prevent IE8 from trying to guess the type of content .

0
source

Change next line

 context.Response.ContentType = "application/xml"; 

from

 context.Response.ContentType = "text/xml"; 
0
source

All Articles