Using a .NET Web Handler to Generate Dynamic XML Feed for Flash
Mark Rasmussen has a really useful XML document fluent interface posted that I have been using for generating XML data feeds for my Flash/Flex projects. While there are certainly alternative methods to acheive the same result, such as LINQ to XML, XMLSerializer or XMLWriter, I have found that Mark's fluent interface is easy to use and has been great for my purposes. What I typically have been doing is creating a web handler, checking the query string for the type of feed required and then sending the appropriate queries to the database to return the desired information that I want to format as XML. The following example certainly has room for improvement - the query string should probably undergo some type of validation and probably a few other things that I haven't even considered yet :-)
Using the interface is as simple as adding his XMLOutput class to your App_Code folder and instantiating it from your page or handler:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
String category = context.Request.QueryString["cat"]; //add validation here
String output;
context.Response.ContentType = "text/xml";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
// Assign the XML document
output = XMLdata(category);
context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Write(output);
}
public bool IsReusable
{
get { return true; }
}
public String XMLdata(String _category)
{
// Send query/parameter and get data
BlogDataSetTableAdapters.ImagesTableAdapter ta = new BlogDataSetTableAdapters.ImagesTableAdapter();
BlogDataSet.ImagesDataTable dt = ta.GetImages(_category);
// Create XML document
XmlOutput xo = new XmlOutput()
.XmlDeclaration()
.Node("images").Within();
foreach (BlogDataSet.ImagesRow row in dt.Rows)
{
String _title = "";
String _filename = "";
String _caption = "";
String _thumbnail = "";
if (row.title != "")
{
_title = row.title;
}
if (row.filename != "")
{
_filename = "images/" + row.filename;
_thumbnail = "thumbs/" + row.filename;
}
if (row.caption != "")
{
_caption = row.caption;
}
xo.Node("pic").Within()
.Node("image").InnerText(_filename)
.Node("title").InnerText(_title)
.Node("thumbnail").InnerText(_thumbnail)
.Node("caption").InnerText(_caption)
.EndWithin();
}
return xo.GetOuterXml();
}
}
An Example of the XML Document Output:
<?xml version="1.0" encoding="utf-8"?>
<images>
<pic>
<image>images/savvy.jpg</image>
<title>tshirt design for local magazine</title>
<thumbnail>thumbs/savvy.jpg</thumbnail>
<caption>tshirt design for local magazine</caption>
</pic>
<pic>
<image>images/Robertsmith.jpg</image>
<title>Robert Smith Tshirt Design</title>
<thumbnail>thumbs/Robertsmith.jpg</thumbnail>
<caption>tshirt design for Robert Smith</caption>
</pic>
<!--//rest of document//-->