first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

ASP.NET MVC: “What is the best way to return XML from a controller’s action in ASP.NET MVC?”; stackoverflow.com

Use MVCContrib's XmlResult Action.

For reference here is their code:

public class XmlResult : ActionResult { private object objectToSerialize;

/// <summary>
/// Initializes a new instance of the <see cref="XmlResult"/> class.
/// </summary>
/// <param name="objectToSerialize">The object to serialize to XML.</param>
public XmlResult(object objectToSerialize)
{
    this.objectToSerialize = objectToSerialize;
}

/// <summary>
/// Gets the object to be serialized to XML.
/// </summary>
public object ObjectToSerialize
{
    get { return this.objectToSerialize; }
}

/// <summary>
/// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
/// </summary>
/// <param name="context">The controller context for the current request.</param>
public override void ExecuteResult(ControllerContext context)
{
    if (this.objectToSerialize != null)
    {
        context.HttpContext.Response.Clear();
        var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
        context.HttpContext.Response.ContentType = "text/xml";
        xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
    }
}

}

[http://stackoverflow.com/questions/134905/what-is-the-best-way- to-return-xml-from-a-controllers-action-in-aspnet-mvc]

[http://www.codeplex.com/MVCContrib]

mod date: 2009-09-16T00:11:39.000Z