在我的MVC視圖(使用VS 2012 RC)中,我試圖解析剛從控制器返回的XML。 這裏是我的視圖代碼:在Asp.net MVC視圖中,如何使用jQuery來解析剛剛從控制器返回的XML
@model RzMvc.Models.PortfolioResponse
@{
ViewBag.Title = "PortfolioList";
}
<script type="text/javascript">
$(displayXmlResponse);
function displayXmlResponse(){
var resp = $("#xmlResp");
$("#xmlTest").append(resp.children());
var xml = $("xmlResp").text;
xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc),
$title = $xml.find("portfolioSummary");
$("#xmlTest").append($title.text());
}
</script>
<h2>PortfolioList</h2>
<div>
<p id="xmlTest"></p>
<p id="xmlResp" >@Model.XmlPortfolioResponse</p>
</div>
瀏覽器看起來就像這樣:
PortfolioList
投資組合清單
System.Xml.XmlDocument
任何指導將是很大的一個ppreciated。下面是我的控制器代碼的一部分:
public ActionResult PortfolioList()
{
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(portfoliosResponse);
var portf = new PortfolioResponse { XmlPortfolioResponse = xmlResponse };
return View(portf);
}
型號代碼:
namespace RzMvc.Models
{
public class PortfolioResponse
{
public XmlDocument XmlPortfolioResponse { get; set; }
}
}
此無關使用JavaScript(jQuery)。您必須在View with C#代碼中處理XML數據(壞主意,我不喜歡它),或者更好的方式是,某種類型的存儲庫應該返回體面的對象,並將XML文件作爲數據源。 – Styxxy