1.新建一般处理处理程序
例子:[C#] 纯文本查看 复制代码
//指定http输出类型
context.Response.ContentType="text/XML";
Book b=new BookServices().GetBookById(Convert.toInt32(context.request.Params["BookId"]));//根据Id获取book的信息,返回book对象
//定义XML输入流(XmlTextWriter(指定二进制格式输出,指定编码格式))
XmlWriter writer=new XmlTextWriter(context.response.outputstream,Encoding.UTF8);
//定义XmlSerializer 将对象序列化到XML文档
(将指定对象反序列化到Xml)
XmlSerializer seriaizer=new XmlSerializer(b.GetType());
//写入文件到XML序列化流中
(流对象,要写入的对象)
seriaizer.Serialize(writer,b);
//关闭流
writer.close();
=====================================================================================================================================================
2.处理HTML页面
[C#] 纯文本查看 复制代码 2.1 JavaScript脚本
function btnClick(){
var bookId=document.getElementById("BookId").value;
var url="_Hander/BookList.aspx?BookId="+bookId;
//定义全局xhr
var xhr;
if(window.ActivexObject){
xhr=new ActivexObjext("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var dom=xhr.responseXML;
document.getElementById("Id").innerHTML =
"编号:"+dom.getElementsByTagName("Id")[0].text;
document.getElementById("Title").innerHTML =
"书名:" + dom.getElementsByTagName("Title")[0].text;
document.getElementById("Author").innerHTML =
"作者:" + dom.getElementsByTagName("Author")[0].text;
document.getElementById("PublisherId").innerHTML =
"出版社:" + dom.getElementsByTagName("PublisherId")[0].text;
document.getElementById("PublishDate").innerHTML =
"出版时间:" + dom.getElementsByTagName("PublishDate")[0].text;
document.getElementById("ISBN").innerHTML =
"ISBN:" + dom.getElementsByTagName("ISBN")[0].text;
document.getElementById("WordsCount").innerHTML =
"WordsCount:" + dom.getElementsByTagName("WordsCount")[0].text;
document.getElementById("UnitPrice").innerHTML =
"UnitPrice:" + dom.getElementsByTagName("UnitPrice")[0].text;
document.getElementById("ContentDescription").innerHTML =
"ContentDescription:" + dom.getElementsByTagName("ContentDescription")[0].text;
document.getElementById("AurhorDescription").innerHTML =
"AurhorDescription:" + dom.getElementsByTagName("AurhorDescription")[0].text;
document.getElementById("EditorComment").innerHTML =
"EditorComment:" + dom.getElementsByTagName("EditorComment")[0].text;
document.getElementById("TOC").innerHTML =
"TOC:" + dom.getElementsByTagName("TOC")[0].text;
document.getElementById("cateGorie").innerHTML =
"cateGorie:" + dom.getElementsByTagName("cateGorie")[0].text;
}
}
xhr.open("GET",url,true);
xhr.send(null);
}
}
=============================================================================================================================================================
3 HTML
[C#] 纯文本查看 复制代码 <body>
<p>
<input id="Text1" type="text" />
<input id="Button1" type="button"
value="button"/></p>
<ul>
<li id="Id"></li>
<li id="Title"></li>
<li id="Author"></li>
<li id="PublisherId"></li>
<li id="PublishDate"></li>
<li id="ISBN"></li>
<li id="WordsCount"></li>
<li id="UnitPrice"></li>
<li id="ContentDescription"></li>
<li id="AurhorDescription"></li>
<li id="EditorComment"></li>
<li id="TOC"></li>
<li id="cateGorie"></li>
</ul>
</body>
|