Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display raw XML from a URL in iframe

Tags:

html

xml

iframe

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.

like image 940
Jai Avatar asked Jul 29 '16 10:07

Jai


2 Answers

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;
 }
like image 172
Avid Programmer Avatar answered Sep 19 '22 03:09

Avid Programmer


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.)

like image 37
leondepdelaw Avatar answered Sep 21 '22 03:09

leondepdelaw