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?
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:
\\myDevServer\...\InetPub\KML
Internet Information Services (IIS) Manager
on your DEV serverKML
folder and choose Properties
HTTP Headers
tabMIME types
buttonNew
OK
twice to get back to the HTTP Headers
tabKML
folder as an ASP.NET application (maybe optional depending on how your server is set up)
Directory
tabCreate
buttonApplication name
field becomes active with the setting KML
OK
taking you back to the main IIS Manager windowEmpty Web Site
C#
\\myDevServer\...\InetPub\KML\
Solution Explorer
New Item
Generic Handler
from the Visual Studio installed templates
windowMelroseVista.ashx
)Visual C#
OK
//
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;
}
}
}
open
or save
the resulting KML file.open
it, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Floridasave
it, 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.