DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 利用Ajax傳遞Xml文檔
利用Ajax傳遞Xml文檔
編輯:XML詳解     

<script language="Javascript">

//生成XML文件
function GetAllFormData()
{
    var strXML = "<ClIEnt>\r\n<FormData>\r\n";
    strXML += "<UserName>bccu</UserName>"
    strXML += "<Age>25</Age>";
    strXML += "</FormData>\r\n</ClIEnt>"
    return strXML;
}
 
///向服務器發送XML文檔
function Send(Str,URL) 
{
    var Http = new ActiveXObject("Microsoft.XMLHTTP")
    Http.open("POST",URL,false)
    Http.send(Str)
    return Http.responseText;
}
 
///獲得XML中指定的節的值
function GetXMLNodeValue(strXML,nodeName)
{
    var Dom = new ActiveXObject("Microsoft.XMLDOM")
    Dom.async=false 
    Dom.loadXML(strXML)
    if(Dom.parseError.errorCode != 0) 
    {
        delete(Dom)
        return(false)
    }
    else
    {
        var node = Dom.documentElement.selectSingleNode("//"+nodeName);
        if(node)
            nodeValue = node.text;
        delete(Dom)
        return(nodeValue);
    }
}

 function Test()
 {
    var tmp       = Send(GetAllFormData(),"./test.ASPx");
    var name      = GetXMLNodeValue(tmp,"UserName");
    var passWord  = GetXMLNodeValue(tmp,"Age");
 }
</script>


服務器端(test.cs)

System.IO.Stream stream = Request.InputStream
System.Xml.XmlDocument doc = new XMLDocument();
try
{
  doc.Load(stream); //加載發送過來的XML文檔
}
catch
{
  byte[] buffer = new byte[stream.Length];
  stream.Read(buffer,0,buffer.Length);
  string strXML = System.Text.UnicodeEncoding.Default.GetString(buffer,0,buffer.Length);
  doc.LoadXml(strXML);
}

//將doc處理後輸出以便返回到客戶端(此處省略)

response.write("")

  • 上一篇文章: XML Schema幫你建模(4) - ASP.Net
  • 下一篇文章: XML“走進”數據庫 - ASP.Net
  • XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
    Copyright © DIV+CSS佈局教程網 All Rights Reserved