日期:2014-05-16 浏览次数:20703 次
public string DataTableToXMLString(System.Data.DataTable dt)
{
if (dt == null) return null;
System.Text.StringBuilder sXML = new System.Text.StringBuilder();
System.IO.StringWriter sWriter = new System.IO.StringWriter(sXML);
System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sWriter);
if (string.IsNullOrEmpty(dt.TableName)) dt.TableName = "row";
try
{
dt.WriteXml(xWriter);
return sXML.ToString();
}
catch { }
finally { sWriter.Close(); xWriter.Close(); }
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
string v1=Request.Form["v1"],v2=Request.Form["v2"];
if (string.IsNullOrEmpty(v1)&&string.IsNullOrEmpty(v2)){
DataTable dt;
//通过v1,v2读取数据库填充DataTable
string xml=DataTableToXMLString(dt);/////
Response.ContentType = "text/xml";//////////
Response.Write(xml);///////
Response.End();///////
}
}
$.ajax({url:'xxxx.aspx',type:'POST',data:'v1=xxxxx&v2=xxxxxx',dataType:'xml'
,success:function(XMLDOM){/*XMLDOM就是xml的dom根节点,自己解析,不会网上查下*/}
,errof:function(xhr){alert('加载XML出错!'+xhr.responseText)}
});