使用ASP.NET AJAX读取XML [英] Read XML using ASP.NET AJAX
问题描述
ResponseFile.aspx.cs
--------------------
ResponseFile.aspx.cs
--------------------
protected void Page_Load(object sender, EventArgs e)
{
string xmlString = "<?xml version=\"1.0\"?><company><employee id=\"001\" >John</employee><employee id=\"002\" >Mike</employee></company>";
Response.Write(xmlString);
}
================================================== =================================
Default.aspx中的JavaScript代码部分
====================================================================================
JavaScript Code Portion in Default.aspx
if(xmlHttpRequest.readyState == 4)
{
var response = xmlHttpRequest.responseXML;
var doc = response.documentElement;
var Suggestion = doc.getElementsByTagName('Suggestion')[0].nodeValue;
document.getElementById('SuggestionBox').innerHTML = Suggestion;
}
我在读取XML数据时遇到问题,这是从ResponseFile.aspx.cs获得的响应.
I am getting problems in reading the XML data which we get as a response from the ResponseFile.aspx.cs
How to read the node employee??
推荐答案
在编写xmlString字符串之前,您需要清除所有响应,否则响应字符串包含html标签,还需要设置内容类型对xml的响应,我建议采用这种方式从服务器获取xml.
Before writing your xmlString string you need to clear all response otherwise response string contain html tags and also you need to set content type of response to xml, I suggest this way to get xml from server.
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
XmlDocument Xdoc = new XmlDocument();
string xmlString = "<?xml version=\"1.0\"?><company><employee id=\"001\" >John</employee><employee id=\"002\" >Mike</employee></company>";
Xdoc.LoadXml(xmlString);
Xdoc.Save(Response.OutputStream);
Response.End();
}
检索XML文件内容的示例
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_xml2 [ ^ ]
Example of Retrieve the content of an XML file
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_xml2[^]
这篇关于使用ASP.NET AJAX读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!