从iframe中的URL显示原始XML

时间:2016-07-29 10:28:25

标签: html xml iframe

我的网络服务器中有一个XML,当我尝试在浏览器中打开它时,它正确地显示为原始xml,但是当尝试使用其url在iframe中显示它时,它显示为字符串而不是原始的XML。

<iframe type="application/xml" src="http://www.w3schools.com/xml/simple.xml"></iframe>

http://jsfiddle.net/qvRzT/8/

请注意,我无法在iframe中加载xml作为内容,因为xml是动态生成的,我只能使用其网址加载iframe。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在我的方案中,来自API响应的XML源将传递给HTML iframe代码源。响应内容类型 text / plain 将在html页面中显示纯XML内容而不进行解析

HTML

<iframe type="text/plain" data-bind="attr: {src: ('api/document/view?token=' + doc.downloadUrl)}"></iframe>

C#API响应

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;
 }