views:

25

answers:

2

I am displaying charts that load the data asynchronously because the searches are the work to fetch the data is quite heavy. The data has to be return as XML to make the chart library happy.
My ActionMethods return a ContentResult with the type set as text/xml. I build my Xml using Linq to XML and call ToString. This works fine but it's not ideal to test.
I have another idea to achieve this which would be to return a view that builds my XML using the XSLT View engine.

I am curious and I always try to do the things "the right way". So how are you guys handling such scenarios?

Do you implement a different ViewEngine (like xslt) to build your XML or do you Build your XML inside your controller (Or the service that serves your controller)?

EDIT :

Since I need this to pass the data to a Charting Library, I have to follow their xml structure. Their notation is not the way i want to build my model classes at all. That's why I build the XML myself using Linq to XML and wonder if a template would be better.
Simple serialization is not what I look for

+2  A: 

Write a custom action result:

public class XmlActionResult : ActionResult
{
    public XmlActionResult(object data)
    {
        Data = data;
    }

    public object Data { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "text/xml";

        // TODO: Use your preferred xml serializer 
        // to serialize the model to the response stream :
        // context.HttpContext.Response.OutputStream
    }
}

And in your controller action:

public ActionResult Index() 
{
    var model = _repository.GetModel();
    return new XmlActionResult(model);
}
Darin Dimitrov
Since it is to pass the data to a Chart Library, I have to follow their xml notation. This notation is not the way i want to build my model classes at all. That's why I build the xml myself using Linq to XML and wonder if a template would be better. Simple serialization is not what I look for
Stephane
Well then don't use XML serializer. Use whatever you want in order to convert your model to a compliant XML in the ExecuteResult method.
Darin Dimitrov
ok, That would make a very specific ActionResult, since I have different type of graph that require different xml structure. But that is still much nicer and testable. Thanks!
Stephane
+1  A: 

I use my own custom ActionResult, which you could modify to your needs.

public class XmlDataResult : ActionResult
    {
        private readonly object _stringToConvertToXml;

        public XmlDataResult(string stringToConvertToXml)
        {
            _stringToConvertToXml = stringToConvertToXml;
        }

        public object StringToConvertToXml
        {
            get { return _stringToConvertToXml; }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (_stringToConvertToXml != null)
            {
                context.HttpContext.Response.Clear();
                context.HttpContext.Response.ContentType = "text/xml";
                context.HttpContext.Response.Write(_stringToConvertToXml);
            }
        }
    }
dove