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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:07:23  来源:igfitidea点击:

How do I generate a KML file in ASP.NET?

c#asp.netkml

提问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

服务器

  1. 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)
    1. Open Internet Information Services (IIS) Manageron your DEV server
    2. Navigate to your DEV site
    3. Right-click the KMLfolder and choose Properties
    4. Click the HTTP Headerstab
    5. Click the MIME typesbutton
    6. Click New
    7. Enter
      • Extension: .kml
      • MIME Type: application/vnd.google-earth.kml+xml
    8. Click OKtwice to get back to the HTTP Headerstab
  2. Set the KMLfolder as an ASP.NET application (maybe optional depending on how your server is set up)
    1. Click the Directorytab
    2. Click the Createbutton
    3. The Application namefield becomes active with the setting KML
    4. Click OKtaking you back to the main IIS Manager window
  1. 将 .kml mimetype 添加到您希望此“文件”所在的文件夹中。说,\\myDevServer\...\InetPub\KML
    Google 的说明仅适用于 Apache
    1. Internet Information Services (IIS) Manager在您的 DEV 服务器上打开
    2. 导航到您的 DEV 站点
    3. 右键单击KML文件夹并选择Properties
    4. 点击HTTP Headers选项卡
    5. 点击MIME types按钮
    6. 点击 New
    7. 进入
      • 扩展名:.kml
      • MIME 类型:application/vnd.google-earth.kml+xml
    8. 单击OK两次以返回HTTP Headers选项卡
  2. KML文件夹设置为 ASP.NET 应用程序(可能是可选的,具体取决于您的服务器的设置方式)
    1. 点击Directory选项卡
    2. 点击Create按钮
    3. Application name字段通过设置变为活动状态KML
    4. 单击OK带您返回主 IIS 管理器窗口

Website

网站

  1. Open VS2008:
    1. File >> New Website
    2. Choose:
      • Empty Web Site
      • Language: C#
      • Location: \\myDevServer\...\InetPub\KML\
  2. In Solution Explorer
    1. Rightclick the website
    2. Choose New Item
    3. Choose Generic Handlerfrom the Visual Studio installed templateswindow
    4. Enter a name (I used MelroseVista.ashx)
    5. Choose Language: Visual C#
    6. Click OK
  3. Paste the following code
  1. 打开VS2008:
    1. 文件>>新网站
    2. 选择:
      • Empty Web Site
      • 语: C#
      • 地点: \\myDevServer\...\InetPub\KML\
  2. Solution Explorer
    1. 右键单击网站
    2. 选择 New Item
    3. Generic HandlerVisual Studio installed templates窗口中选择
    4. 输入一个名字(我用过MelroseVista.ashx
    5. 选择语言: Visual C#
    6. 点击 OK
  3. 粘贴以下代码

//

//

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;
        }
    }
}
  1. Attempt to load your page in your favorite browser
  2. You shouldget a popup asking you to openor savethe resulting KML file.
  3. If you openit, you shouldhave GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida
  4. If you saveit, you shouldsee the following in the file
  1. 尝试在您喜欢的浏览器中加载您的页面
  2. 应该会看到一个弹出窗口,要求您输入opensave生成 KML 文件。
  3. 如果你愿意open,你应该让 GoogleEarth 自行启动并放大到佛罗里达州东部的图钉
  4. 如果你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 树中的多个文件夹中。