2

In my MVC view (using VS 2012 RC), I'm attempting to parse the XML freshly returned from my Controller. Here is my view code:

@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>

The browser output looks like this:

PortfolioList

Portfolio Listing

System.Xml.XmlDocument

Any guidance would be greatly appreciated. Here's part of my Controller code:

    public ActionResult PortfolioList()
{
    XmlDocument xmlResponse = new XmlDocument();
    xmlResponse.LoadXml(portfoliosResponse);
    var portf = new PortfolioResponse { XmlPortfolioResponse = xmlResponse };


    return View(portf);
}

Model code is:

namespace RzMvc.Models
{

    public class PortfolioResponse
    {
        public XmlDocument XmlPortfolioResponse { get; set; }
    }
}
4

3 回答 3

1

The problem is that you are returning a PortfolioResponse object, but setting the value of its XmlPortfolioResponse property to be equal to the XmlDocument itself, rather than it's output. This makes the default C# output when you bind to the screen occur - which is to simply call PortfolioResponse.XmlPortfolioResponse.ToString() - which will, unless overloaded, return the name of the Object's class. Hense - you are getting System.Xml.XmlDocument output to your HTML.

What you need to do, first and foremost, is bind @Model.XmlPortfolioResponse.OuterXml to your view page, rather than @Model.XmlPortfolioResponse. You will also most likely have to wrap this value in a @Html.Raw() call in order to get it to actually render appropriately.

Your new binding in the View page might look something like this:

<div class='XmlTest'></div>
<div class='XmlPortfolioResponse'>
 @Html.Raw(Model.XmlPortfolioResponse.OuterXml.ToString())
</div>

For your JQuery, then, to parse it, you would do something like:

var $title = $('.XmlPortfolioResponse').find('portfolioSummary').text();
$('.XmlTest').text($title);

However, you might consider a more elegant approach than binding raw XML into your page, then parsing it / reformatting it / re-displaying it - which leads to DOM bloat and a lot of extra work in processing. I would suggest one of 2 approaches:

1) Output XML and style the XML directly. CSS is more than capable of styling the XML you're outputting and making it appear as you wish - assuming you aren't making major changes to format. An example might look like:

<div class='XmlPortfolioResponse'>
  <portfolioSummary>
    <value>123456.78</value>
    <assets>
      <asset>
        <ticker>XYZ</ticker>
        <quantity>50</quantity>
        <value>456.78</value>
      </asset>
    </assets>
  </portfolioSummary>
</div>

If you have this kind of XML - low on attributes, but well normalized - you could write a stylesheet like:

portfolioSummary {
    display: block;
    border: 1px solid #eee;
    background: #fff;
    position: relative;
    margin-top: 25px;
    width: 100%;
    height: 100%;
    /* Whatever other styles you want for layout */
}
portfolioSummary value {
    float: right;
    font-weight: bold;
    position: absolute;
    top: 5px; right: 5px;
}
portfolioSummary asset {
    display: block;
}
/* Etc. */

In other words - directly style the XML.

2) If, however, you have XML like the following:

<div class='XmlPortfolioResponse'>
  <portfolioSummary value='123456.78'>
   <asset value='456.78' ticker='XYZ'>
   </asset>
  </portfolioSummary>
</div>

In other words, heavy on attributes which don't lend themselves to styling... Consider loading the data via AJAX. Your JQuery would then change to something like:

$.ajax({
    url: '/Portfolio/Load/12345',
    type: 'POST',
    async: true,
    success: function (data) {
        if (!data) // do validation
            alert('failed!');
        else {
            var $xml = $(data);
            var $title = $xml.find('portfolioSummary').text();
            $('.XmlTest').text($title);
        }
    },
    error: function (error) {
        alert('failed!');
    }
});

In order to use the $.ajax() method at the bottom, you need a controller method which is something like the following:

[HttpGet] // Original Method - allows GET
public ActionResult PortfolioList {
    return View(GetPortfolios()); // Or however you load them...
}

[HttpPost] // Allows this method to be accessed via POST calls
[ActionName("PortfolioList")]
public JsonResult PortfolioList_Async {        
    return Json(GetPortfolios()); // You may need to resolve serialization here,
                                  // but usually it works well out of the box.
}
于 2012-06-25T21:53:08.873 回答
0

You'll need to convert your XmlDocument to a string before you send it to the client. The easiest way to do this is to call .OuterXml.

于 2012-06-25T21:26:35.983 回答
0

In controller itself convert the xml data into object of your need like list of portfoliosummaries and then pass that object using viewdata or viewbag to view.

 XmlNodeList elemList =xmlresponse.DocumentElement.GetElementsByTagName("portfoliosummary");
            List<string> lstportfoliosummarys= new List<string>();
            for (int i = 0; i < elemList.Count; i++)
            {
                if (elemList[i].ChildNodes[1].InnerText == "portfoliosummary")
                    lstEmails.Add(elemList[i].ChildNodes[2].InnerText);
            }
            ViewBag.contacts = new SelectList(lstportfoliosummarys);

Hope this will help you.

于 2012-06-25T21:40:40.037 回答