前台:
復制代碼 代碼如下:
<!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文件然後發向服務器</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");//只改了這裡,原來是loadXML("");
sendXml( xmlDoc,'Default.aspx');
}
//向服務器發送Xml文檔
function sendXml(xmlDoc,serverURL){
xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");
var strDoc;
if (typeof(xmlDoc) == "object")//這裡的判斷是需要的.這裡仍然需要加個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;
//這裡接受服務器傳來的Xml文檔,轉化成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']");//這裡的值需要加引號
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文件然後發向服務器</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");//只改了這裡,原來是loadXML("");
sendXml( xmlDoc,'Default.aspx');
}
//向服務器發送Xml文檔
function sendXml(xmlDoc,serverURL){
xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP.3.0");
var strDoc;
if (typeof(xmlDoc) == "object")//這裡的判斷是需要的.這裡仍然需要加個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;
//這裡接受服務器傳來的Xml文檔,轉化成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']");//這裡的值需要加引號
alert(singleNode.text);
}
}
// --></script>
</head>
<body>
<input type="button" onclick="f();" value="request" />
</body>
</html>
Ajax
復制代碼 代碼如下:
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);//接收到客戶端傳來的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);//接收到客戶端傳來的xml
xmldoc.Save(Server.MapPath("~"+"/hello.xml"));
Response.Write(xmldoc.InnerXml);//返回修改後的Xml文檔
Response.End();
}
}
Xml文檔:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<person id="1">
<name>tree</name>
<gender>male</gender>
</person>
</root>