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//-->