前臺(tái):
復(fù)制代碼 代碼如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" >
head>
title>js裝載xml文件然后發(fā)向服務(wù)器/title>
script type="text/javascript">!--
var xmlHttp = null;
function f(){
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.load("xmlfile.xml");//只改了這里,原來(lái)是loadXML("");
sendXml( xmlDoc,'Default.aspx');
}
//向服務(wù)器發(fā)送Xml文檔
function sendXml(xmlDoc,serverURL){
xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");
var strDoc;
if (typeof(xmlDoc) == "object")//這里的判斷是需要的.這里仍然需要加個(gè)xml后綴
strDoc = xmlDoc.xml;
else
strDoc = xmlDoc;
xmlHttp.open ("POST","Default.aspx" ,true);
xmlHttp.onreadystatechange=getData;
xmlHttp.send(strDoc);
}
function getData(){
if (xmlHttp.readyState==4)
{
var strxml=xmlHttp.responseText;
//這里接受服務(wù)器傳來(lái)的Xml文檔,轉(zhuǎn)化成xml文檔.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.loadXML(strxml);
//alert(xmlDoc.xml);
var singleNode = xmlDoc.selectSingleNode("/root/person[gender='male']");//這里的值需要加引號(hào)
alert(singleNode.text);
}
}
// -->/script>
/head>
body>
input type="button" onclick="f();" value="request" />
/body>
/html>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" >
head>
title>js裝載xml文件然后發(fā)向服務(wù)器/title>
script type="text/javascript">!--
var xmlHttp = null;
function f(){
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.load("xmlfile.xml");//只改了這里,原來(lái)是loadXML("");
sendXml( xmlDoc,'Default.aspx');
}
//向服務(wù)器發(fā)送Xml文檔
function sendXml(xmlDoc,serverURL){
xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");
var strDoc;
if (typeof(xmlDoc) == "object")//這里的判斷是需要的.這里仍然需要加個(gè)xml后綴
strDoc = xmlDoc.xml;
else
strDoc = xmlDoc;
xmlHttp.open ("POST","Default.aspx" ,true);
xmlHttp.onreadystatechange=getData;
xmlHttp.send(strDoc);
}
function getData(){
if (xmlHttp.readyState==4)
{
var strxml=xmlHttp.responseText;
//這里接受服務(wù)器傳來(lái)的Xml文檔,轉(zhuǎn)化成xml文檔.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.loadXML(strxml);
//alert(xmlDoc.xml);
var singleNode = xmlDoc.selectSingleNode("/root/person[gender='male']");//這里的值需要加引號(hào)
alert(singleNode.text);
}
}
// -->/script>
/head>
body>
input type="button" onclick="f();" value="request" />
/body>
/html>
Ajax
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class testXml_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Request.InputStream);//接收到客戶端傳來(lái)的xml
xmldoc.Save(Server.MapPath("~"+"/hello.xml"));
Response.Write(xmldoc.InnerXml);//返回修改后的Xml文檔
Response.End();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class testXml_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Request.InputStream);//接收到客戶端傳來(lái)的xml
xmldoc.Save(Server.MapPath("~"+"/hello.xml"));
Response.Write(xmldoc.InnerXml);//返回修改后的Xml文檔
Response.End();
}
}
Xml文檔:
復(fù)制代碼 代碼如下:
?xml version="1.0" encoding="utf-8" ?>
root>
person id="1">
name>tree/name>
gender>male/gender>
/person>
/root>