所有
我写了一个方法,扩展了SolrNet示例MVC应用程序中的一些功能。这很简单,在我的控制器中我添加了以下内容。
public string Index() { 尝试 { var solr = ServiceLocator.Current.GetInstance>(); String 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&gt;();由于某些原因?我整天都在调试器中走过,似乎无法弄清楚为什么它会把整个事情写出来两次。它确实利用了foreach循环并通过它进行了正确迭代,但同样重复了整个文件的内容。
对此的任何帮助都会很棒! 亚当
答案 0 :(得分:1)
呃......我完全忽视了我两次写缓冲区的事实!
Response.Write(buffer.ToString());
和
return buffer.ToString();
亚当