Create Dynamic XML Files with ASP.NET HTTP Handlers
The internet in it’s current state sees XML as a very mature technology to allow data to be transferred between sites. There may be many times when you will want to generate XML files in your web solutions and ASP.NET provides HTTP Handlers to assist in this task. The ASHX files that you create in your ASP.NET solutions will handle a request and allow you to output the file as a different type.
In our example, we’ll pretend you are a user car dealership and you’ll send back the current cars you have for sale. To make things easier, I’ll create some small objects to represent our cars.
C#
public class Car
{
private string _make;
private int _year;
public string Make
{
get { return this._make; }
set { this._make = value; }
}
public string Year
{
get { return this._year; }
set { this._year = value; }
}
}
VB.NET
Public Class Car
Dim _make As String
Dim _year As String
Public Property Make() As String
Get
Return Me._make
End Get
Set(ByVal value As String)
Me._make = value
End Set
End Property
Public Property Year() As String
Get
Return Me._year
End Get
Set(ByVal value As String)
Me._year = value
End Set
End Property
End Class
Now that we have our car objects, let’s output our XML. Generally when outputting XML it ends up being an exercise in looping many things (arrays, collections, etc). In our simple case, we will create an arraylist and add out car objects to it and then loop to create the XML.
C#
// Create the arraylist
using System.Collections;
ArrayList list = new ArrayList();
Car audi = new Car();
audi.Make = “Audi";
audi.Year = “2003″;
list.Add(audi);
Car honda = new Car();
honda.Make = “Honda";
honda.Year = “2005″;
list.Add(honda);
VB.NET
Dim list As ArrayList = New ArrayList()
Dim audi As Car = New Car()
audi.Make = “Audi"
audi.Year = “2003″
list.Add(audi)
Dim honda As Car = New Car()
honda.Make = “Honda"
honda.Year = “2005″
list.Add(honda)
Now that we have an ArrayList to work with, we loop through it to create the XML. Note the need to set the ContentType of the file (in this case text/xml) as well as the xml header.
C#
context.Response.ContentType = "text/xml";
context.Response.Write("<?xml version=\"1.0\"?>\n");
context.Response.Write("<cars>");
foreach (Car car in list)
{
context.Response.Write("<car>");
context.Response.Write("<make>" + car.Make + “</make>");
context.Response.Write("<year>" + car.Year + “</year>");
context.Response.Write("</car>");
}
context.Response.Write("</cars>");
VB.NET
context.Response.ContentType = "text/xml"
context.Response.Write("<?xml version=""1.0″"?>")
context.Response.Write("<cars>")
For Each tcar As Car In list
context.Response.Write("<car>")
context.Response.Write("<make>" + tcar.Make + “</make>")
context.Response.Write("<year>" + tcar.Year + “</year>")
context.Response.Write("</car>")
Next
context.Response.Write("</cars>")
That’s about it. The example was simple, but you can extend it to create XML based on your site’s needs, such as an RSS feed of some sort. Please let me know if you have any questions or if you want to see a VB.NET version of this code. Thanks!
Leave a Comment
If you would like to make a comment, please fill out the form below.
Yes i am interested to see the VB.NET version of this code,i will be thankful to you. (u can mail me on above mail-id)
If anyone needs to learn on how to create XML files with PHP for auto sitemaps, let me know and I will post a tutorial here!
thanks for sharing good information