乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      WebBrowser控件顯示XML流 - 令狐沖和酒壺 - 博客園

       旭龍 2011-01-10

      WebBrowser控件顯示XML流

              VS2005里集成的WebBrowser控件,就是一個(gè)IE瀏覽器,如果想用它來(lái)相顯示XML字符串,并達(dá)到IE的效果,我總結(jié)了一下,有如下幾種方法:
              文件顯示法:可以以文件的形式顯示!將你要顯示的XML串存到本地文件里,并將WebBrowser的Url屬性指向這個(gè)文件即可!這種方法簡(jiǎn)易,有效,但不一定實(shí)用,因?yàn)榇蟛糠謺r(shí)間我們要做的工作是將一個(gè)XML流顯示出來(lái)。當(dāng)然,我們可以將XML流存到一個(gè)臨時(shí)文件里,之后再用第一種方法來(lái)顯示這個(gè)XML串;但是,我們還有更好的方法,不用在本地生成臨時(shí)文件!
              首先,看看IE是怎樣顯示XML串的。當(dāng)我們用IE打開(kāi)XML文件時(shí),在IE里顯示的是一個(gè)按XML文檔格式縮進(jìn)的,并可以折起和展開(kāi)的XML字符串。其實(shí)IE只是用它內(nèi)嵌的XSLT文件,將XML轉(zhuǎn)成了我們看到的html代碼。那么同理,我們也可以將我們要顯示的XML字符串用XSLT轉(zhuǎn)換成html代碼,并賦給WebBrowser的DocumentText屬性。如果你是XSLT高手,那么你可以自己寫(xiě)一段XSLT,之后用如下代碼來(lái)完成工作:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Xml;
      using System.Xml.Xsl;
      using System.IO;

      namespace WindowsApplication
      {
          
      public partial class Form1 : Form
          
      {
              
      public Form1()
              
      {
                  InitializeComponent();
              }


              
      private void button1_Click(object sender, EventArgs e)
              
      {
                  
      string   xml = String.Empty;                                    //Xml字符串
                  string   xslt = String.Empty;                                   //Xslt字符串

                  XmlReader reader 
      = null;                                        //要轉(zhuǎn)換的Xml
                  MemoryStream readerstream = new MemoryStream();                 //要轉(zhuǎn)換的Xml流

                  XmlWriter writer 
      = null;                                        //轉(zhuǎn)換后的字符串
                  MemoryStream writerstream = new MemoryStream();                 //轉(zhuǎn)換后的字符串流  
                             
                  XslCompiledTransform trans 
      = new XslCompiledTransform();        //Xslt對(duì)象        System.Xml.Xsl命名空間下

                  MemoryStream stream 
      = new MemoryStream();                       //Xslt流    
                  XmlReader xsltreader = null;

                  
      string returnValue = String.Empty;                              //轉(zhuǎn)換后的html代碼  
                  
                  
      byte[] byteArray = Encoding.UTF8.GetBytes(xslt);

                  
      try
                  
      {
                      
      //取得Xslt流    
                      stream.Write(byteArray, 0, byteArray.Length);
                      xsltreader 
      = XmlReader.Create(stream);



                      
      //取得要轉(zhuǎn)換的Xml流
                      byte[] byteXml = Encoding.UTF8.GetBytes(xml);
                      readerstream.Write(byteXml,
      0,byteXml.Length);
                      reader 
      = XmlReader.Create(readerstream);


                      
      //取得轉(zhuǎn)換后的字符串流
                      writer = XmlWriter.Create(writerstream);

                      trans.Load(xsltreader);

                      trans.Transform(reader, writer);


                      writerstream.Position 
      = 0;
                      returnValue 
      = new StreamReader(writerstream).ReadToEnd();

                      
      this.webBrowser1.DocumentText = returnValue;                //將html代碼賦給WebBrowser的DocumentText屬性


                  }

                  
      finally
                  
      {
                      readerstream.Close();
                      writerstream.Close();
                      stream.Close();

                      
      if (reader != null)
                      
      {
                          reader.Close();
                      }


                      
      if (writer != null)
                      
      {
                          writer.Close();
                      }


                      
      if (xsltreader != null)
                      
      {
                          xsltreader.Close();
                      }


                  }


              }

          }

      }

              如果很不幸,你跟我一樣,對(duì)XSLT并不十分精通,也沒(méi)有關(guān)系,你可以用Microsoft提供的defaultss.xsl文件,網(wǎng)上有很多文章介紹怎么得到這個(gè)文件。不過(guò),如果你使用defaultss.xsl這個(gè)文件的話(huà),就不能再用以上方法了,因?yàn)?font face="Verdana">XslCompiledTransform類(lèi)的Load方法,只能讀取符合x(chóng)slt 1.0標(biāo)準(zhǔn)的XSLT文件,但defualtss.xsl并不完全符合這個(gè)標(biāo)準(zhǔn)。那么,這時(shí),我們可以采用另外一種方法,那就是引用COM!你可以引Microsoft XML 3.0 Parser(如果你本機(jī)安裝了的話(huà)),并使用如下方法完成工作:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Xml;
      using System.Xml.Xsl;
      using System.IO;

      namespace WindowsApplication
      {
          
      public partial class Form1 : Form
          
      {
              
      public Form1()
              
      {
                  InitializeComponent();
              }


              
      private void button1_Click(object sender, EventArgs e)
              
      {
                  MSXML2.DOMDocument30Class xml 
      = new MSXML2.DOMDocument30Class();        //Xml對(duì)象
                  MSXML2.DOMDocument30Class xslt = new MSXML2.DOMDocument30Class();       //Xslt對(duì)象    

                  xml.loadXML(
      "");                                                        //讀取Xml字符串
                  xslt.loadXML("");                                                       //讀取Xslt字符串

                  
      this.webBrowser1.DocumentText = xml.transformNode(xslt);                //將轉(zhuǎn)換的html字符串賦給WebBrowser控件
              }

          }

      }

              如果你不想有臨時(shí)文件,也不想引用COM的話(huà),那么還有如下方法可供選擇,呵呵!
              正如前邊所說(shuō),WebBrowser就像個(gè)IE,那么既然是IE,就會(huì)支持javascript,那么我們可以以客戶(hù)端調(diào)用的方式來(lái)實(shí)現(xiàn)Xml文件的轉(zhuǎn)換

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      <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>
                  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;
                
      </SCRIPT>
               
      <script language="javascript">
                  function showXML()
                  
      {
                      var xml 
      = null;             //xml 對(duì)象
                      var xsl = null;             //xsl 對(duì)象
                      var xslstring = null;       //xslt 字符串
                      
                      xml 
      = new ActiveXObject("Microsoft.XMLDOM");
                      xml.loadXML(
      "@@XML");                               //讀取XML字符串
                                
                      xslstring 
      = "<?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=\"/\"><DIV class=\"st\"><x:apply-templates/></DIV></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></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></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>";
                                  
                      xsl 
      = new ActiveXObject("Microsoft.XMLDOM");       
                      xsl.loadXML(xslstring);                               
      //讀取XSL字符串     
                      
                      document.all(
      "xml").innerHTML = xml.transformNode(xsl);
                      
                  }

              
      </script>
          
      </head>
          
      <body onload="showXML();">
              
      <div id="xml"></div>    
          
      </body>
      </html>

              這段html文件其實(shí)是一個(gè)模板,你可以把它存在Resource文件里,之后在向WebBrowser的DocumentText屬性賦值之前,將模板里的@@XML用你實(shí)際想顯示的XML串替換一下,同時(shí)沒(méi)忘了將這個(gè)XML串里的"替成\"!

              又寫(xiě)完了一篇心得,希望能對(duì)大家有所幫助

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多