How to return an XML string as an action result in MVC [duplicate] How to return an XML string as an action result in MVC [duplicate] xml xml

How to return an XML string as an action result in MVC [duplicate]


You could use return this.Content(xmlString, "text/xml"); to return a built XML string from an action.


For JSON/XML I have written an XML/JSON Action Filter that makes it very easy to tackle without handling special cases in your action handler (which is what you seem to be doing).


Another way to do this is by using XDocument:

using System.Xml.Linq;public XDocument ExportXml(){    Response.AddHeader("Content-Type", "text/xml");    return XDocument.Parse("<xml>...");}