C# 如何在 ASP.NET 中生成 KML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/952667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I generate a KML file in ASP.NET?
提问by Sukotto
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?
如何生成 KML 文档并将其直接返回到浏览器,而无需将临时文件写入服务器或依赖 3rd 方库或类?
采纳答案by Mehrdad Afshari
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 ProcessRequestmethod. The general approach is good, though.
我建议您考虑使用 HTTP 处理程序而不是 ASP.NET 页面。它将更清洁、更高效。只需将“通用处理程序”类型的新项目添加到您的项目中,并考虑将代码移动到其ProcessRequest方法中。不过,一般方法是好的。
By the way, unless you are explicitly mapping .kmlfiles to an ASP.NET handler, it'll not run anyway. I suggest going with the default .ashxextension and add a Content-DispositionHTTP header to set the filename for the client:
顺便说一句,除非您将.kml文件显式映射到 ASP.NET 处理程序,否则它无论如何都不会运行。我建议使用默认.ashx扩展名并添加一个Content-DispositionHTTP 标头来为客户端设置文件名:
Response.AddHeader("Content-Disposition", "attachment; filename=File.kml");
Also, note that you should set header stuff beforeanything is sent to the client so you should move setting Content-Typeand adding header before other stuff.
另外,请注意,您应该在将任何内容发送到客户端之前设置标题内容,因此您应该Content-Type在其他内容之前移动设置和添加标题。
Full Solution (From the OP):
完整解决方案(来自 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
- 将 .kml mimetype 添加到您希望此“文件”所在的文件夹中。说,
\\myDevServer\...\InetPub\KML
(Google 的说明仅适用于 Apache)Internet Information Services (IIS) Manager在您的 DEV 服务器上打开- 导航到您的 DEV 站点
- 右键单击
KML文件夹并选择Properties - 点击
HTTP Headers选项卡 - 点击
MIME types按钮 - 点击
New - 进入
- 扩展名:.kml
- MIME 类型:application/vnd.google-earth.kml+xml
- 单击
OK两次以返回HTTP Headers选项卡
- 将
KML文件夹设置为 ASP.NET 应用程序(可能是可选的,具体取决于您的服务器的设置方式)- 点击
Directory选项卡 - 点击
Create按钮 - 该
Application name字段通过设置变为活动状态KML - 单击
OK带您返回主 IIS 管理器窗口
- 点击
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
- 打开VS2008:
- 文件>>新网站
- 选择:
Empty Web Site- 语:
C# - 地点:
\\myDevServer\...\InetPub\KML\
- 在
Solution Explorer- 右键单击网站
- 选择
New Item Generic Handler从Visual Studio installed templates窗口中选择- 输入一个名字(我用过
MelroseVista.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;
}
}
}
- Attempt to load your page in your favorite browser
- You shouldget a popup asking you to
openorsavethe resulting KML file. - If you
openit, you shouldhave GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida - If you
saveit, you shouldsee the following in the file
- 尝试在您喜欢的浏览器中加载您的页面
- 您应该会看到一个弹出窗口,要求您输入
open或save生成 KML 文件。 - 如果你愿意
open,你应该让 GoogleEarth 自行启动并放大到佛罗里达州东部的图钉 - 如果你
save是,你应该在文件中看到以下内容
\
\
<?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: XmlTextWriterworked pretty well here. However, I think XMLDocumentlooks 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.
注意:XmlTextWriter在这里工作得很好。但是,我认为XMLDocument对于较大的 KML 文件看起来更有希望,因为您可以在将其推送给用户之前在内存中对其进行操作。例如,如果您希望同一点出现在 GoogleEarth Locations 树中的多个文件夹中。

