序列中不包含mvc3 GoogleMaps.LocationServices元素

时间:2014-01-21 14:25:10

标签: xml asp.net-mvc-3 google-maps

我有异常的问题。当我在MVC3中使用XmlElement创建XML文件时,我有时会收到此错误(5次中的1次)

  

序列不包含元素

     

[InvalidOperationException:Sequence不包含任何元素]
  System.Linq.Enumerable.First(IEnumerable`1 source)+336
  GoogleMaps.LocationServices.GoogleLocationService.GetLatLongFromAddress(字符串   地址)+185

在我的控制器中,我有这段代码:

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();
        }

任何人都知道出了什么问题?

1 个答案:

答案 0 :(得分:0)

这些xml文件的生成速度有多快? Google服务都有请求率限制,因此您可能会遇到此问题。尝试在每次调用GoogleLocationService之间等待(100分钟左右应该这样做)并查看是否可以解决问题。