飘落的枫叶 发表于 2008-12-9 09:55:36

灵活调用xsl来解析xml文档

1.新建一个vs2003的web工程,取名为XMLTest<BR><BR>   2.将工程目录下的WebForm1.aspx中内容全部删除,只留下顶部的一条语句:<BR><BR>   &lt;%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="XMLTest.WebForm1" %&gt;<BR><BR><BR>3.修改WebForm1.aspx.cs中内容,在Page_Load中加入:<BR><BR>

    XmlDocument doc=new XmlDocument(); <BR>    String xmlfile=string.Empty; <BR>    xmlfile=Context.Request.PhysicalApplicationPath (Request.QueryString["sel"].ToString()=="xml"?"\\hello.xml":"\\hello.xsl"); <BR>    doc.Load(xmlfile); <BR>    Response.Write(doc.InnerXml);<BR><BR>   4.在工程根目录下新增test.htm,并设为工程首页:<BR><BR>

&lt;html&gt; <BR>&lt;head&gt; <BR>&lt;title&gt;&lt;/title&gt; <BR>&lt;/head&gt; <BR>&lt;body&gt; <BR>&lt;div id="resTree"&gt;&lt;/div&gt; <BR>&lt;FONT face="宋体"&gt;&lt;/FONT&gt;&lt;input type="button" value="执行" onclick="GetXml()"&gt;&lt;BR&gt; <BR>&lt;script language="JScript"&gt; <BR>var srcTree,xsltTree,xt; <BR>var http_request = false; <BR>    <BR>function GetXml() <BR>{   <BR>   srcTree = new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); <BR>    srcTree.async=false; <BR>    xsltTree= new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); <BR>    xsltTree.async = false; <BR>    xt=new ActiveXObject("MSXML2.XSLTemplate"); <BR>   resTree.innerHTML=""; <BR>    makeRequest("WebForm1.aspx?sel=xml",GetXml_CB); <BR>} <BR>   <BR>    function makeRequest(url,callback) { <BR>      http_request = false; <BR>      if (window.XMLHttpRequest) { // Mozilla, Safari,... <BR>            http_request = new XMLHttpRequest(); <BR>            if (http_request.overrideMimeType) { <BR>                http_request.overrideMimeType('text/xml'); <BR>            } <BR>      } else if (window.ActiveXObject) { // IE <BR>            try { <BR>                http_request = new ActiveXObject("Msxml2.XMLHTTP"); <BR>            } catch (e) { <BR>                try { <BR>                  http_request = new ActiveXObject("Microsoft.XMLHTTP"); <BR>                } catch (e) {} <BR>            } <BR>      } <BR><BR>      if (!http_request) { <BR>            alert('Giving up :( Cannot create an XMLHTTP instance'); <BR>
页: [1]
查看完整版本: 灵活调用xsl来解析xml文档