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"?>
    <Name>John Hammond</Name>
    <Country>United States</Country>
    <Name>Mudassar Khan</Name>
    <Name>Suzanne Mathews</Name>
    <Name>Robert Schidner</Name>
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();
    public ContentResult AjaxMethod()
        string xml = System.IO.File.ReadAllText(Server.MapPath("~/XML/Customers.xml"));
        return Content(xml);
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>
    <meta name="viewport" content="width=device-width"/>
    <input type="button" id="btnGet" value="Get XML"/>
    <textarea id="txtXml" rows="25" cols="40" style="border:none; overflow:hidden"></textarea>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGet").click(function () {
                    type: "POST",
                    url: "/Home/AjaxMethod",
                    data: '',
                    contentType: "application/json; charset=utf-8",
                    dataType: "text",
                    success: function (response) {
                    failure: function (response) {
                    error: function (response) {
Return XML file from Controller's Action method to View in ASP.Net MVC