XML学习(三) js保存xml

.aspx页利用XMLHTTPrequest发送修改过的xml,在接受也接收保存。

主要语句

 xmlHttp.open("POST", "receive.aspx?type=xmlsave", true); 

 xmlHttp.send(xmlDoc);

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
< 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" >
< div >
      <span id="to"></span>
</ div >
</ form >
</ body >
</ html >

 

receive.aspx.cs

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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 " );

 

转载于:https://www.cnblogs.com/princeback/archive/2010/08/12/1797816.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值