用javascript裝載xml發到Server

來源:互聯網
上載者:User

(Browser)

  1. <html xmlns="http://www.w3.org/1999/xhtml" >  
  2. <head>  
  3.     <title>js裝載xml檔案然後發向伺服器</title>  
  4.     <script type="text/javascript"><!--   
  5.     var xmlHttp = null;   
  6.     function xiaofang(){   
  7.         var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");   
  8.         xmlDoc.async = false;   
  9.         xmlDoc.load("xmlfile.xml");//只改了這裡,原來是loadXML("");   
  10.            
  11.         sendXml( xmlDoc,'Default.aspx');   
  12.     }   
  13.     //向伺服器發送Xml文檔   
  14.     function sendXml(xmlDoc,serverURL){   
  15.         xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");   
  16.         var strDoc;   
  17.         if (typeof(xmlDoc) == "object")//這裡的判斷是需要的.這裡仍然需要加個xml尾碼   
  18.            strDoc = xmlDoc.xml;   
  19.         else   
  20.            strDoc = xmlDoc;   
  21.            
  22.         xmlHttp.open ("POST","Default.aspx" ,true);   
  23.         xmlHttp.onreadystatechange=getData;   
  24.         xmlHttp.send(strDoc);    
  25.     }   
  26.     function getData(){   
  27.         if (xmlHttp.readyState==4)   
  28.         {   
  29.             var strxml=xmlHttp.responseText;   
  30.             //這裡接受伺服器傳來的Xml文檔,轉化成xml文檔.   
  31.             var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");   
  32.             xmlDoc.async = false;   
  33.             xmlDoc.loadXML(strxml);   
  34.             //alert(xmlDoc.xml);   
  35.             var singleNode = xmlDoc.selectSingleNode("/root/person[gender='fang']");//這裡的值需要加引號   
  36.             alert(singleNode.text);   
  37.         }   
  38.     }   
  39.        
  40. // --></script>  
  41. </head>  
  42. <body>  
  43. <input type="button" onclick="xiaofang();" value="request" />  
  44. </body>  
  45. </html>  

 

(Server)

  1.  
    1. using System;   
    2. using System.Data;   
    3. using System.Configuration;   
    4. using System.Collections;   
    5. using System.Web;   
    6. using System.Web.Security;   
    7. using System.Web.UI;   
    8. using System.Web.UI.WebControls;   
    9. using System.Web.UI.WebControls.WebParts;   
    10. using System.Web.UI.HtmlControls;   
    11.   
    12. using System.Xml;   (注意命名空間)
    13.   
    14. public partial class testXml_Default : System.Web.UI.Page   
    15. {   
    16.     protected void Page_Load(object sender, EventArgs e)   
    17.     {   
    18.         XmlDocument xmldoc = new XmlDocument();   
    19.         xmldoc.Load(Request.InputStream);//接收到用戶端傳來的xml   
    20.         xmldoc.Save(Server.MapPath("~"+"/hello.xml"));   
    21.         Response.Write(xmldoc.InnerXml);//返回修改後的Xml文檔   
    22.         Response.End();   
    23.     }   
    24. }  

 

 (Xml檔案)

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <root>  
  3.   <person id="1">  
  4.     <name>xiao</name>  
  5.     <gender>fang</gender>  
  6.   </person>  
  7. </root>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.