How do I generate and return a KML document directly to the browser without writing a temporary file to the server or relying on a 3rd party library or class?
1 Answers
I suggest you consider using an HTTP Handler instead of a ASP.NET page. It will be cleaner and more performant. Just add new item of type "Generic Handler" to your project and consider moving the code to its ProcessRequest method. The general approach is good, though.
By the way, unless you are explicitly mapping .kml files to an ASP.NET handler, it'll not run anyway. I suggest going with the default .ashx extension and add a Content-Disposition HTTP header to set the filename for the client:
Response.AddHeader("Content-Disposition", "attachment; filename=File.kml");
Also, note that you should set header stuff before anything is sent to the client so you should move setting Content-Type and adding header before other stuff.
Full Solution (From the OP):
Here's how I did it:
Server
- Add the .kml mimetype to the folder where you want this "file" to live. Say,
\\myDevServer\...\InetPub\KML
(Google's instructions are only for Apache)- Open
Internet Information Services (IIS) Manageron your DEV server - Navigate to your DEV site
- Right-click the
KMLfolder and chooseProperties - Click the
HTTP Headerstab - Click the
MIME typesbutton - Click
New - Enter
- Extension: .kml
- MIME Type: application/vnd.google-earth.kml+xml
- Click
OKtwice to get back to theHTTP Headerstab
- Open
- Set the
KMLfolder as an ASP.NET application (maybe optional depending on how your server is set up)- Click the
Directorytab - Click the
Createbutton - The
Application namefield becomes active with the settingKML - Click
OKtaking you back to the main IIS Manager window
- Click the
Website
- Open VS2008:
- File >> New Website
- Choose:
Empty Web Site- Language:
C# - Location:
\\myDevServer\...\InetPub\KML\
- In
Solution Explorer- Rightclick the website
- Choose
New Item - Choose
Generic Handlerfrom theVisual Studio installed templateswindow - Enter a name (I used
MelroseVista.ashx) - Choose Language:
Visual C# - Click
OK
- Paste the following code
//
using System;
using System.Web;
using System.Xml;
public class Handler : IHttpHandler
{
public void ProcessRequest( HttpContext context)
{
context.Response.ContentType = "application/vnd.google-earth.kml+xml";
context.Response.AddHeader("Content-Disposition", "attachment; filename=MelroseVista.kml");
XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
kml.Formatting = Formatting.Indented;
kml.Indentation = 3;
kml.WriteStartDocument();
kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
kml.WriteStartElement("Placemark");
kml.WriteElementString("name", "Melrose Vista FL");
kml.WriteElementString("description", "A nice little town");
kml.WriteStartElement("Point");
kml.WriteElementString("coordinates", "-80.18451400000000000000,26.08816400000000000000,0");
kml.WriteEndElement(); // <Point>
kml.WriteEndElement(); // <Placemark>
kml.WriteEndDocument(); // <kml>
kml.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
- Attempt to load your page in your favorite browser
- You should get a popup asking you to
openorsavethe resulting KML file. - If you
openit, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida - If you
saveit, you should see the following in the file
\
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Melrose Vista FL</name>
<description>A nice little town</description>
<Point>
<coordinates>-80.18451400000000000000,26.08816400000000000000,0</coordinates>
</Point>
</Placemark>
</kml>
Note: XmlTextWriter worked pretty well here. However, I think XMLDocument looks more promising for larger KML files since you can manipulate it in memory before pushing it to the user. If, for example, you want the same point to appear in multiple folders in the GoogleEarth Locations tree.
- 25,759
- 22
- 127
- 221
- 414,610
- 91
- 852
- 789
-
I'll look into using an HTTP handler thanks. Do you have a favorite, specific example? Assuming I end up going with what I have, would you point me towards an answer #2 above? My background is mostly perl hacking and I'm having trouble getting my head around integrating the code-behind to the actual page the browser's asking for. – Sukotto Jun 04 '09 at 20:42
-
hmm... comments don't preserve vertical whitespace :-( – Sukotto Jun 04 '09 at 20:43
-
You don't need an example. Just right click on the project -> Add new item -> Generic Handler. It'll create a template for you. Copy and paste the code in Page_Load above to the ProcessRequest method. The only thing you need to change is adding a "var Response = context.Response" at the beginning of the method. I found one anyway: http://stackoverflow.com/questions/873207/force-download-of-a-file-on-web-server-asp-net-c/873228#873228 – Mehrdad Afshari Jun 04 '09 at 20:55