一个简单的例子:
ReturnXML.aspx文件内容
<%@ Page language="c#"
Codebehind="ReturnXML.aspx.cs"
AutoEventWireup="false"
Inherits="Testonly.ReturnXML"
%>
ReturnXML.aspx.cs文件内容
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace Testonly
{
/// <summary>
/// ReturnXML 的摘要说明。
/// </summary>
public class ReturnXML : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// XmlDocument xmlDoc = new XmlDocument();
// xmlDoc.Load(Request.InputStream);
//这里可以取客户端Post过来的数据
//
// string sType = xmlDoc.SelectSingleNode("OilMIS/Code"
.Attributes.Item(0).Value;
// string sCanID = xmlDoc.SelectSingleNode("OilMIS/Code"
.Attributes.Item(1).Value;
StringBuilder sbXML = new StringBuilder();
//这里添加代码从数据库取数
sbXML.Append(string.Format("<Code tankNo='111' tankName='测试数据{0}' />",DateTime.Now.ToLongTimeString()));
//*********************************************
Response.ContentType = "Text/Xml";
//Response.Write( "<?xml version=/"1.0/"
encoding=/"utf-8/"
?> "
;
Response.Write ("<OilMIS>"
;
Response.Write (sbXML.ToString());
Response.Write ("</OilMIS>"
;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
另外一个HTMLPage1.htm:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR"
content="Microsoft Visual Studio .NET 7.1">
<meta name="ProgId"
content="VisualStudio.HTML">
<meta name="Originator"
content="Microsoft Visual Studio .NET 7.1">
</head>
<body>
<script language="javascript">
<!--
function DataCommunicate(strXML,ActionFileURL)
{
var xmlDoc = new ActiveXObject("MSXML.DOMDocument"
;
xmlDoc.async = false;
strXML = "<OilMIS>"
+ strXML + "</OilMIS>";
var httpObj = new ActiveXObject("Microsoft.XMLHTTP"
;
if(xmlDoc.loadXML(strXML))
{
httpObj.Open("POST",ActionFileURL,false);
httpObj.Send(xmlDoc);
if(xmlDoc.loadXML(httpObj.responseText)==false)
{
return "<OilMIS>FALSE</OilMIS>";
}
else
{
return xmlDoc.xml;
}
}
}
function GetData()
{
var sReceiver = DataCommunicate("","ReturnXML.aspx"
;
var xmlDoc = new ActiveXObject("Msxml.DOMDocument"
;
alert(sReceiver);
xmlDoc.loadXML(sReceiver);
var nodelist = xmlDoc.selectNodes("OilMIS/Code"
;
alert(nodelist[0].attributes[1].value);
//return nodelist;
lblTest.innerHTML += nodelist[0].attributes[0].value + nodelist[0].attributes[1].value + "<br>";
window.setTimeout("GetData();",6000);
}
window.setTimeout("GetData();",2000);
//-->
</script>
<span id="lblTest">
</span>
</body>
</html>
你浏览一下HTMLPage1.htm