I have a XML in my webserver, when i try to open it in a browser it displays properly as raw xml, but the same when tried to display it in an iframe with its url, it displays as string and not as raw xml.
<iframe type="application/xml" src="http://www.w3schools.com/xml/simple.xml"></iframe>
http://jsfiddle.net/qvRzT/8/
Please note that I cannot load xml as a content in iframe because the xml is dynamically generated, I can only use its url to load in iframe.
In my scenario, the XML source from API response will be passed to HTML iframe tag source. Response content type text/plain will display plain XML content in html page without parsing
HTML
<iframe type="text/plain" data-bind="attr: {src: ('api/document/view?token=' + doc.downloadUrl)}"></iframe>
C# API response
public HttpResponseMessage View(string token)
{
HttpResponseMessage result = null;
var localFilePath ="D:\sample.xml";
var fileInfo = new System.IO.FileInfo(localFilePath);
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
string FileMimeType = MimeMapping.GetMimeMapping(fileInfo.Name);
if (FileMimeType == "text/xml")
{
result.Content.Headers.ContentType = new
System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
}
else
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(FileMimeType);
result.Content.Headers.ContentDisposition = new
System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
result.Content.Headers.ContentDisposition.FileName = fileInfo.Name;
return result;
}
This is because the browser sees the XML as the source of the page. So it will be marked up as an XML file. When the browser gets the iframe and loads the XML, it handles the source as HTML. (Even if no HTML tag is provided.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With