如何用WebBrowser显示语法加亮的XML源代码(不用文件)?

onet 2003-01-10 05:09:39
我想要的效果是这样的:将XML源码存在StringList或Stream中,然后将Stream传给WebBrowser,WebBrowser能象IE打开xml文件那样进行语法加亮显示,完全不通过本地文件。

我现在能做到的是用navigate实现对本地xml文件的加亮显示,或者对html进行stream方式的显示。但是用stream将xml传给WebBrowser显示为部分文本,没有语法加亮,内容也不全。

期待高手相助,分还可再加!
...全文
72 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
halfdream 2003-03-03

<?xml version="1.0"?>
<x:stylesheet xmlns:x="http://www.w3.org/TR/WD-xsl" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:d2="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<x:template match="/">
<HTML><HEAD>
<STYLE>BODY{font:x-small 'Verdana';margin-right:1.5em}
.c{cursor:hand}
.b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none}
.e{margin-left:1em;text-indent:-1em;margin-right:1em}
.k{margin-left:1em;text-indent:-1em;margin-right:1em}
.t{color:#990000}
.xt{color:#990099}
.ns{color:red}
.dt{color:green}
.m{color:blue}
.tx{font-weight:bold}
.db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier}
.di{font:small Courier}
.d{color:blue}
.pi{color:blue}
.cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888}
.ci{font:small Courier;color:#888888}
PRE{margin:0px;display:inline}</STYLE>
<SCRIPT><x:comment>
function f(e){
if (e.className=="ci"){if (e.children(0).innerText.indexOf("\n")>0) fix(e,"cb");}
if (e.className=="di"){if (e.children(0).innerText.indexOf("\n")>0) fix(e,"db");}
e.id="";
}
function fix(e,cl){
e.className=cl;
e.style.display="block";
j=e.parentElement.children(0);
j.className="c";
k=j.children(0);
k.style.visibility="visible";
k.href="#";
}
function ch(e){
mark=e.children(0).children(0);
if (mark.innerText=="+"){
mark.innerText="-";
for (var i=1;i<e.children.length;i++)
e.children(i).style.display="block";
}
else if (mark.innerText=="-"){
mark.innerText="+";
for (var i=1;i<e.children.length;i++)
e.children(i).style.display="none";
}}
function ch2(e){
mark=e.children(0).children(0);
contents=e.children(1);
if (mark.innerText=="+"){
mark.innerText="-";
if (contents.className=="db"||contents.className=="cb")
contents.style.display="block";
else contents.style.display="inline";
}
else if (mark.innerText=="-"){
mark.innerText="+";
contents.style.display="none";
}}
function cl(){
e=window.event.srcElement;
if (e.className!="c"){e=e.parentElement;if (e.className!="c"){return;}}
e=e.parentElement;
if (e.className=="e") ch(e);
if (e.className=="k") ch2(e);
}
function ex(){}
function h(){window.status=" ";}
document.onclick=cl;
</x:comment></SCRIPT>
</HEAD>
<BODY class="st"><x:apply-templates/></BODY>
</HTML>
</x:template>
<x:template match="node()[nodeType()=10]">
<DIV class="e"><SPAN>
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="d"><!DOCTYPE <x:node-name/><I> (View Source for full doctype...)</I>></SPAN>
</SPAN></DIV>
</x:template>
<x:template match="pi()">
<DIV class="e">
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m"><?</SPAN><SPAN class="pi"><x:node-name/>
<x:value-of/></SPAN><SPAN class="m">?></SPAN>
</DIV>
</x:template>
<x:template match="pi('xml')">
<DIV class="e">
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m"><?</SPAN><SPAN class="pi">xml <x:for-each select="@*"><x:node-name/>="<x:value-of/>" </x:for-each></SPAN><SPAN class="m">?></SPAN>
</DIV>
</x:template>
<x:template match="@*" xml:space="preserve"><SPAN><x:attribute name="class"><x:if match="x:*/@*">x</x:if>t</x:attribute> <x:node-name/></SPAN><SPAN class="m">="</SPAN><B><x:value-of/></B><SPAN class="m">"</SPAN></x:template>
<x:template match="@xmlns:*|@xmlns|@xml:*"><SPAN class="ns">
<x:node-name/></SPAN><SPAN class="m">="</SPAN><B class="ns"><x:value-of/></B><SPAN class="m">"</SPAN></x:template>
<x:template match="@dt:*|@d2:*"><SPAN class="dt">
<x:node-name/></SPAN><SPAN class="m">="</SPAN><B class="dt"><x:value-of/></B><SPAN class="m">"</SPAN></x:template>
<x:template match="textnode()">
<DIV class="e">
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="tx"><x:value-of/></SPAN>
</DIV>
</x:template>
<x:template match="comment()">
<DIV class="k">
<SPAN><A class="b" onclick="return false" onfocus="h()" STYLE="visibility:hidden">-</A>
<SPAN class="m"><!--</SPAN></SPAN>
<SPAN id="clean" class="ci"><PRE><x:value-of/></PRE></SPAN>
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m">--></SPAN>
<SCRIPT>f(clean);</SCRIPT></DIV>
</x:template>
<x:template match="cdata()">
<DIV class="k">
<SPAN><A class="b" onclick="return false" onfocus="h()" STYLE="visibility:hidden">-</A>
<SPAN class="m"><![CDATA[</SPAN></SPAN>
<SPAN id="clean" class="di"><PRE><x:value-of/></PRE></SPAN>
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m">]]></SPAN>
<SCRIPT>f(clean);</SCRIPT></DIV>
</x:template>
<x:template match="*">
<DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m"><</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN>
<x:apply-templates select="@*"/><SPAN class="m"> /></SPAN>
</DIV></DIV>
</x:template>
<x:template match="*[node()]">
<DIV class="e">
<DIV class="c"><A href="#" onclick="return false" onfocus="h()" class="b">-</A>
<SPAN class="m"><</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN><x:apply-templates select="@*"/>
<SPAN class="m">></SPAN></DIV>
<DIV><x:apply-templates/>
<DIV><SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m"></</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN><SPAN class="m">></SPAN></DIV>
</DIV></DIV>
</x:template>
<x:template match="*[textnode()$and$$not$(comment()$or$pi()$or$cdata())]">
<DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
<SPAN class="m"><</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN><x:apply-templates select="@*"/>
<SPAN class="m">></SPAN><SPAN class="tx"><x:value-of/></SPAN><SPAN class="m"></</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN><SPAN class="m">></SPAN>
</DIV></DIV>
</x:template>
<x:template match="*
  • ">
    <DIV class="e">
    <DIV class="c" STYLE="margin-left:1em;text-indent:-2em"><A href="#" onclick="return false" onfocus="h
  • ()" class="b">-</A>
    <SPAN class="m"><</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN><x:apply-templates select="@*"/>
    <SPAN class="m">></SPAN></DIV>
    <DIV><x:apply-templates/>
    <DIV><SPAN class="b"><x:entity-ref name="nbsp"/></SPAN>
    <SPAN class="m"></</SPAN><SPAN><x:attribute name="class"><x:if match="x:*">x</x:if>t</x:attribute><x:node-name/></SPAN><SPAN class="m">></SPAN></DIV>
    </DIV></DIV>
    </x:template>
    </x:stylesheet>
回复
LMXEQ5 2003-02-28
halfdream(哈欠) 兄,不知你是如何取得IE缺省的XSL的,可以与大家分享吗,谢谢!!!
回复
halfdream 2003-02-11
成了!!:)
IE在打开XML文档的时候,如果这个XML文档指定了XSL,就进行XSL转换得到HTML码显示
如果这个XML文档未指定,将用一个缺省XSL来将其转成HTML用于显示。

当初以为IE是直接显示XML码,再一想,不可能的,这样从软件构架上来看
它直接显示XML是得不偿失的。

明白这点就好办了。接下来是做下面工作
1,取得IE的这个缺省的XSL (很容易,我倒是花了不少功夫,先自己想)
2,自己使用XML DOM进行XML的XSL转换得HTML码(楼主也许会)
3,将HTML码写入IE (这个楼主比我熟)


感谢这问题,几次尝试,
让我明白了不少关于IE文档对象模型和XML文档对象模型的东西。

回复
halfdream 2003-02-11
想了又想,试了又试,终于想出了一个思路,等试成功了再说。
回复
Eastunfail 2003-01-31
关注
回复
halfdream 2003-01-31
关注。
回复
ricetons 2003-01-31
你想也是可以的。在输出之前,加一句:
<meta http-equiv='Content-Type' content='text/xml'>
回复
feels30000 2003-01-13
只有扩展名为.XML时,浏览器才会做此处理,我想你这样做有什么很重要的意义吗
回复
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

1565

社区成员

Delphi 网络通信/分布式开发
申请成为版主
社区公告
暂无公告