序列不包含mvc3 GoogleMaps.LocationServices的元素 [英] Sequence contains no elements mvc3 GoogleMaps.LocationServices
问题描述
XmlElement
创建一个XML文件时,我有时会遇到这个错误(5次中的1次)。
序列不包含元素
System.Linq.Enumerable.First(IEnumerable `1 source)+336
GoogleMaps.LocationServices.GoogleLocationService.GetLatLongFromAddress(String
address)+185
在我的控制器中,我有以下代码:使用GoogleMaps.LocationServices;
public ActionResult Index()
{
XmlDocument doc = new XmlDocument();
XmlDeclaration documentType = doc.CreateXmlDeclaration(1.0,utf-8,null);
doc.AppendChild(documentType);
XmlElement markers = doc.CreateElement(markers);
doc.AppendChild(markers);
var locationService = new GoogleLocationService();
foreach(var repository.Mreza.ToList()中的项)
{
XmlElement marker = doc.CreateElement(marker);
XmlElement name = doc.CreateElement(name);
XmlElement address = doc.CreateElement(address);
XmlElement tel = doc.CreateElement(tel);
XmlElement fax = doc.CreateElement(fax);
XmlElement time2 = doc.CreateElement(time2);
XmlElement more = doc.CreateElement(more);
XmlElement lat = doc.CreateElement(lat);
XmlElement lng = doc.CreateElement(lng);
name.InnerText = item.Ime;
address.InnerText = item.Ulica;
tel.InnerText = item.Telefon;
fax.InnerText = item.Fax;
time2.InnerText = item.Email;
more.InnerText = item.Web;
var point = locationService.GetLatLongFromAddress(item.Grad);
lat.InnerText = Convert.ToString(point.Latitude);
lng.InnerText = Convert.ToString(point.Longitude);
marker.AppendChild(name);
marker.AppendChild(address);
marker.AppendChild(tel);
marker.AppendChild(fax);
marker.AppendChild(time2);
marker.AppendChild(more);
marker.AppendChild(lat);
marker.AppendChild(lng);
markers.AppendChild(marker);
}
string URL = Server.MapPath(Content / NovoLayout / adreseXML.xml);
doc.Save(URL);
return View();
}
任何人都知道什么是错的?
这些xml文件生成的速度有多快? Google服务都有请求速率限制,因此您可能会遇到这种情况。尝试在GoogleLocationService的每次调用之间施加一次等待(100毫秒左右应该这样做),然后查看是否可以解决问题。
I have unusual problem. When I create an XML file with XmlElement
in MVC3, I sometimes get this error (1 of 5 times)
Sequence contains no elements
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.First(IEnumerable`1 source) +336
GoogleMaps.LocationServices.GoogleLocationService.GetLatLongFromAddress(String address) +185
In my controller I have this code:
using GoogleMaps.LocationServices;
public ActionResult Index()
{
XmlDocument doc = new XmlDocument();
XmlDeclaration documentType = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(documentType);
XmlElement markers = doc.CreateElement("markers");
doc.AppendChild(markers);
var locationService = new GoogleLocationService();
foreach (var item in repository.Mreza.ToList())
{
XmlElement marker = doc.CreateElement("marker");
XmlElement name = doc.CreateElement("name");
XmlElement address = doc.CreateElement("address");
XmlElement tel = doc.CreateElement("tel");
XmlElement fax = doc.CreateElement("fax");
XmlElement time2 = doc.CreateElement("time2");
XmlElement more = doc.CreateElement("more");
XmlElement lat = doc.CreateElement("lat");
XmlElement lng = doc.CreateElement("lng");
name.InnerText = item.Ime;
address.InnerText = item.Ulica;
tel.InnerText = item.Telefon;
fax.InnerText = item.Fax;
time2.InnerText = item.Email;
more.InnerText = item.Web;
var point = locationService.GetLatLongFromAddress(item.Grad);
lat.InnerText = Convert.ToString(point.Latitude);
lng.InnerText = Convert.ToString(point.Longitude);
marker.AppendChild(name);
marker.AppendChild(address);
marker.AppendChild(tel);
marker.AppendChild(fax);
marker.AppendChild(time2);
marker.AppendChild(more);
marker.AppendChild(lat);
marker.AppendChild(lng);
markers.AppendChild(marker);
}
string URL = Server.MapPath("Content/NovoLayout/adreseXML.xml");
doc.Save(URL);
return View();
}
Anyone knows what is wrong?
How quickly are these xml files generated? The Google services all have request rate limits so you're likely coming up against this. Try imposing a wait between each call to the GoogleLocationService (100ms or so should do) and see if this fixes the issue.
这篇关于序列不包含mvc3 GoogleMaps.LocationServices的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!