Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET MVC, How to return an xml document to a View

Tags:

asp.net-mvc

I'd like some guidance in returning an XML doc from a controller to a view. In my view, I'd like to traverse the XML doc using JQuery. There are plenty of online examples using JQuery for this use.

I have a PortfolioList() Controller below, which right now just returns the view, but I would like to figure out how to RETURN THE XML RESPONSE. You'll noticed below that I'm writing the XML response to a local file just for testing purposes...

Do I need to cleanly create a model for this ?

    public ActionResult PortfolioList()
    {
        XmlDocument xmlResponse = new XmlDocument();
        XmlDocument xmlRequest = new XmlDocument();

        bool rzInitialized = nitializeRz();
        if (rzInitialized == false)
        {
            ViewBag.Message = "Rz Init has failed. Check if Rz is running";
            return View();
        }
        bool rzConnected = ConnectToRz();    

        ViewBag.Message = "Here you may view a list of portfolios and exposures.";

        // Build Portfolio Select request here !
        RequestBuilder rzRequest = new RequestBuilder();

        // REQUEST FOR PORTFOLIOS !
        string portfoliosRequest = rzRequest.PortfoliosRequest("Portfolios");
        string **portfoliosResponse** = RzClient.sendRequest(portfoliosRequest, false);

        // DEBUG REQUESTS !!
        if (Debugflag)
        {
            rzRequest.DebugOutput("portfolios", portfoliosRequest, portfoliosResponse);
        }
        DisconnectFromRz();

        return View("PortfolioList");
    }
like image 709
bob.mazzo Avatar asked Jun 22 '12 15:06

bob.mazzo


1 Answers

You can do it as follows.

public ActionResult PortfolioList()
{
    //Your code
    ....
    return this.Content(yourXml, "text/xml");
}
like image 131
Kaf Avatar answered Oct 19 '22 17:10

Kaf