www.RickLaFleur.com

 

Simple way to output XML in ASP.NET MVC

Another post to document something that I really don't want to have to look up ever again. I simply wanted to output XML to the browser window using ASP.NET MVC. Sounds easy, simply use:

public ContentResult Index()
{
    StringWriter writer = new StringWriter();
    myXmlDocument.Save(writer);
    return this.Content(writer.ToString(), @"text/xml", writer.Encoding);
}

But no luck; IE7's CSS would not display the XML since IIS ASP.NET defaults to UTF-16 and the previous page was UTF-8? Yes, both pages were correctly tagged with there encoding and correctly identified by IE as UTF-8 or UTF-16. It just wouldn't process the later. Whats up with that; can't these MS kids get along. So did a search and found a soluiton posted by Robert McLaw using a modified StringWriter that accepted an encoding which would worked very nicely:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;
public StringWriterWithEncoding(Encoding encoding)
{
  this.encoding = encoding;
}
public override Encoding Encoding
{
  get { return encoding; }
}
}

To implement, just use the new writer and set it's encoding as desired:

public ContentResult Index()
{
   StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8);
   myXmlDocument.Save(writer);
   return this.Content(writer.ToString(), @"text/xml", writer.Encoding);
}

Labels:


Comments:
Would it not be better to use an XmlResult instead? Something like:

using System;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Linq;

public class XmlResult : ActionResult
{
public XmlResult()
{
this.ContentEncoding = Encoding.UTF8;
this.ContentType = "text/xml";
}

public XNode Content { get; set; }
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }

public override void ExecuteResult(ControllerContext context)
{
if (null == context) throw new ArgumentNullException("context");

HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
if (null != ContentEncoding)
{
response.ContentEncoding = ContentEncoding;
}
if (null != Content)
{
using (var writer = XmlWriter.Create(response.Output))
{
Content.WriteTo(writer);
}
}
}
}
 
nice
 
I would appreciate more visual materials, to make your blog more attractive, but your writing style really compensates it. But there is always place for improvement
 
Not bad article, but I really miss that you didn't express your opinion, but ok you just have different approach
 
Thanks for the Alternate XmlResult; it's a nice alternative and even better in a MVC solution.

The StringWriterWithEncoding option posted can be used in additional solutions.
 
Post a Comment



Links to this post:

Create a Link



<< Home

Archives

January 2007  |   March 2007  |   May 2007  |   February 2008  |   April 2008  |   July 2008  |   September 2008  |   October 2008  |   December 2008  |   January 2009  |   April 2009  |   May 2009  |   June 2009  |   December 2009  |   January 2010  |  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]