Subversion Repository Public Repository

Nextrek

Diff Revisions 104 vs 105 for /csvTojSon/csvTojSon/Types.cs

Diff revisions: vs.
  @@ -11,6 +11,14 @@
11 11
12 12 namespace csvTojSon
13 13 {
14 + public class cfgSettings
15 + {
16 + public static string bingMapsApiUrl = string.Empty;
17 + public static string apiKeyBingMaps = string.Empty;
18 + public static string fileScarti = string.Empty;
19 + public static string xmlNameSpace = string.Empty;
20 + }
21 +
14 22 public enum GoogleMapsResult
15 23 {
16 24 SUCCESS = 0,
  @@ -20,10 +28,6 @@
20 28
21 29 public class Types
22 30 {
23 - protected static string bingMapsApiUrl = "http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}&o=xml";
24 - protected static string apiKeyBingMaps = "AnrOE7-8e0saApeic_ztGiXnQdsIpzrpGJMN6ohzf5m6-7IO22TtvV25SOGeYGNr";
25 - protected static string xmlNameSpace = "http://schemas.microsoft.com/search/local/ws/rest/v1";
26 -
27 31 [System.Serializable]
28 32 [System.Runtime.Serialization.DataContract]
29 33 public class CSV
  @@ -76,42 +80,48 @@
76 80
77 81 public static GeocoderLocation RecuperaCoordinate(string query)
78 82 {
83 + GeocoderLocation geo = null;
84 +
79 85 //uso la classe System.Net.WebClient per inviare la richiesta GET al servizio REST
80 86 using (var client = new WebClient())
81 87 {
82 88 System.Collections.Specialized.NameValueCollection nvc = System.Configuration.ConfigurationManager.AppSettings;
89 + cfgSettings.fileScarti = (nvc.AllKeys.Contains("fileScarti") ? nvc["fileScarti"] : string.Empty);
90 + cfgSettings.bingMapsApiUrl = (nvc.AllKeys.Contains("bingMapsApiUrl") ? nvc["bingMapsApiUrl"] : string.Empty);
91 + cfgSettings.apiKeyBingMaps = (nvc.AllKeys.Contains("apiKeyBingMaps") ? nvc["apiKeyBingMaps"] : string.Empty);
92 +
93 + var url = string.Format(cfgSettings.bingMapsApiUrl, query.Replace(" ", "+"), cfgSettings.apiKeyBingMaps);
83 94
84 - if (nvc.AllKeys.Contains("bingMapsApiUrl"))
95 + try
85 96 {
86 - bingMapsApiUrl = nvc["bingMapsApiUrl"];
97 + //il risultato è un documento XML
98 + var risultato = client.DownloadString(url);
99 +
100 + //Lo vado a leggere con un XmlDocument
101 + var doc = new XmlDocument();
102 + doc.LoadXml(risultato);
103 +
104 + //Se voglio interrogare il documento, devo almeno caricare il namespace di default
105 + var nsmgr = new XmlNamespaceManager(doc.NameTable);
106 + nsmgr.AddNamespace("ns", cfgSettings.xmlNameSpace);
107 +
108 + //Ora sono pronto, estraggo solo i nodi che mi interessano
109 + var location = doc.DocumentElement.SelectSingleNode("//ns:Point", nsmgr);
110 + if (location != null)
111 + {
112 + geo = new GeocoderLocation
113 + {
114 + Longitude = double.Parse(location.SelectSingleNode("ns:Longitude", nsmgr).InnerText, CultureInfo.InvariantCulture),
115 + Latitude = double.Parse(location.SelectSingleNode("ns:Latitude", nsmgr).InnerText, CultureInfo.InvariantCulture)
116 + };
117 + }
87 118 }
88 - if (nvc.AllKeys.Contains("apiKeyBingMaps"))
119 + catch (Exception ex)
89 120 {
90 - apiKeyBingMaps = nvc["apiKeyBingMaps"];
121 + if (cfgSettings.fileScarti.Trim() != "") System.IO.File.AppendAllText(cfgSettings.fileScarti, ex.Message);
91 122 }
92 123
93 - var url = string.Format(bingMapsApiUrl, query.Replace(" ", "+"), apiKeyBingMaps);
94 -
95 - //il risultato è un documento XML
96 - var risultato = client.DownloadString(url);
97 -
98 - //Lo vado a leggere con un XmlDocument
99 - var doc = new XmlDocument();
100 - doc.LoadXml(risultato);
101 -
102 - //Se voglio interrogare il documento, devo almeno caricare il namespace di default
103 - var nsmgr = new XmlNamespaceManager(doc.NameTable);
104 - nsmgr.AddNamespace("ns", xmlNameSpace);
105 -
106 - //Ora sono pronto, estraggo solo i nodi che mi interessano
107 - var location = doc.DocumentElement.SelectSingleNode("//ns:Point", nsmgr);
108 - if (location == null) return null;
109 -
110 - return new GeocoderLocation
111 - {
112 - Longitude = double.Parse(location.SelectSingleNode("ns:Longitude", nsmgr).InnerText),
113 - Latitude = double.Parse(location.SelectSingleNode("ns:Latitude", nsmgr).InnerText)
114 - };
124 + return geo;
115 125 }
116 126 }
117 127 }