In this article I will explain with an example, how to return XML file from Action method of Controller to View in ASP.Net MVC Razor.
ContentResult return type is used for returning XML file as XML string from Controller to View in ASP.Net MVC Razor.
 
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
XML File
The following is the XML file consisting of records of Customers. The XML file is saved with the name Customers.xml in a folder named XML inside the project folder.
<?xml version="1.0" standalone="yes"?>
<Customers>
 <Customer>
    <Id>1</Id>
    <Name>John Hammond</Name>
    <Country>United States</Country>
 </Customer>
 <Customer>
    <Id>2</Id>
    <Name>Mudassar Khan</Name>
    <Country>India</Country>
 </Customer>
 <Customer>
    <Id>3</Id>
    <Name>Suzanne Mathews</Name>
    <Country>France</Country>
 </Customer>
 <Customer>
    <Id>4</Id>
    <Name>Robert Schidner</Name>
    <Country>Russia</Country>
 </Customer>
</Customers>
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling AJAX POST operation
This Action method handles the call made from the jQuery AJAX function from the View.
Note: The following Action method handles AJAX calls and since a String value is returned, the return type is set to ContentResult.
 
The XML file is read as string value into a variable using the ReadAllText method of the File class. The read XML string is finally returned back to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ContentResult AjaxMethod()
    {
        string xml = System.IO.File.ReadAllText(Server.MapPath("~/XML/Customers.xml"));
        return Content(xml);
    }
}
 
 
View
The View consists of an HTML TextArea element and a Button. The Button has been assigned a jQuery click event handler and when the Button is clicked a jQuery AJAX called is made to the Controller’s action method.
Note: For more details on using jQuery AJAX in ASP.Net MVC, please refer my article ASP.Net MVC: Call Controller Method from View using jQuery AJAX.
 
The URL for the jQuery AJAX call is set to the Controller’s action method i.e. /Home/AjaxMethod. The XML string returned in the response is displayed in the TextArea element.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <input type="button" id="btnGet" value="Get XML"/>
    <hr/>
    <textarea id="txtXml" rows="25" cols="40" style="border:none; overflow:hidden"></textarea>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGet").click(function () {
                $.ajax({
                    type: "POST",
                    url: "/Home/AjaxMethod",
                    data: '',
                    contentType: "application/json; charset=utf-8",
                    dataType: "text",
                    success: function (response) {
                        $("#txtXml").val(response);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>
</body>
</html>
 
 
Screenshot
Return XML file from Controller's Action method to View in ASP.Net MVC
 
 
Downloads