主要语句
xmlhttp.open(post, receive.aspx?type=xmlsave, true);
xmlhttp.send(xmldoc);
代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function parsexml() {
try //internet explorer
{
xmldoc = new activexobject("microsoft.xmldom");
}
catch (e) {
try //firefox, mozilla, opera, etc.
{
xmldoc = document.implementation.createdocument("", "", null);
}
catch (e) {
alert(e.message);
return;
}
}
xmldoc.async = false; //假如xml载入完毕执行以下
xmldoc.load("note.xml");
xmldoc.getelementsbytagname("to")[0].childnodes[0].nodevalue = "yaomingming";
var xmlhttp;
try {
// firefox, opera 8.0+, safari
xmlhttp = new xmlhttprequest();
}
catch (e) {
// internet explorer
try {
xmlhttp = new activexobject("msxml2.xmlhttp");
}
catch (e) {
try {
xmlhttp = new activexobject("microsoft.xmlhttp");
}
catch (e) {
alert("您的浏览器不支持ajax!");
return false;
}
}
}
xmlhttp.onreadystatechange = function() { //onreadystatechange 属性存有处理服务器响应的函数
if (xmlhttp.readystate == 4) { //readystate 属性存有服务器响应的状态信息
document.getelementbyid("to").innerhtml = xmlhttp.responsetext; //通过 responsetext 属性来取回由服务器返回的数据
}
}
xmlhttp.open("post", "receive.aspx?type=xmlsave", true);
// open() 方法需要三个参数。第一个参数定义发送请求所使用的方法(get 还是 post)。第二个参数规定服务器端脚本的 url。第三个参数规定应当对请求进行异步地处理。
xmlhttp.send(xmldoc); //send() 方法可将请求送往服务器
}
</script>
</head>
<body onload="parsexml()">
<form id="form1" runat="server">
<p>
<span id="to"></span>
</p>
</form>
</body>
</html>
receive.aspx.cs
system.io.stream instream = page.request.inputstream;
binaryreader br = new binaryreader(instream, system.text.encoding.utf8);
byte[] byt = br.readbytes((int)instream.length);
string sxml = system.text.encoding.utf8.getstring(byt);
system.xml.xmldocument xmldoc = new system.xml.xmldocument();
xmldoc.loadxml(sxml);
xmldoc.save(server.mappath("note.xml"));
response.write("save");
以上就是xml学习(三) js保存xml的示例代码分享的详细内容。
