全部,
我编写了一个方法来扩展 SolrNet 示例 MVC 应用程序中的一些功能。这非常简单,在我的控制器中我添加了以下内容。
公共字符串 Index() { 尝试 { var solr = ServiceLocator.Current.GetInstance>(); 字符串 qs = Request.QueryString["q"]; var results = solr.Query(new SolrQueryByField("asciiname", qs));
StringBuilder buffer = new StringBuilder();
buffer.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
buffer.Append("<kml xmlns=\"http://www.opengis.net/kml/2.2\"\n");
buffer.Append(" xmlns:gx=\"http://www.google.com/kml/ext/2.2\">\n");
buffer.Append("<Document>\n");
buffer.Append("<name>\n");
buffer.Append("Gazetteer\n");
buffer.Append("</name>\n");
buffer.Append("<description>\n");
buffer.Append("World Places\n");
buffer.Append("</description>\n");
foreach (var i in results)
{
String lat = i.latitude.ToString();
String lon = i.longitude.ToString();
String name = i.asciiName.ToString();
String coords = lon.ToString() + "," + lat.ToString();
name = name.Replace("&", "&");
name = name.Replace("<", "<");
name = name.Replace(">", ">");
buffer.Append("<Placemark>\n");
buffer.Append("<name>\n");
buffer.Append(name);
buffer.Append("</name>\n");
buffer.Append("<Point>\n");
buffer.Append("<coordinates>\n");
buffer.Append(coords);
buffer.Append("</coordinates>\n");
buffer.Append("</Point>\n");
buffer.Append("</Placemark>\n");
}
buffer.Append("</Document>\n");
buffer.Append("</kml>");
Response.Write(buffer.ToString());
Response.AddHeader("content-disposition", "attachment; filename=geonames.kml");
Response.ContentType = "application/vnd.google-earth.kml+xml";
Response.AppendHeader("Content-Encoding", "kml");
return buffer.ToString();
}
catch (Exception)
{
return "ERROR";
}
}
KML 文件已生成,但内容从标记开始重复。这是否发生在 var solr = ServiceLocator.Current.GetInstance>(); 由于某些原因?我整天都在调试调试器,似乎无法弄清楚为什么它会写出整个事情两次。它确实利用了 foreach 循环并正确地遍历它,但同样,它复制了整个文件的内容。
对此的任何帮助都会很棒!亚当