2012-12-11 3 views
0

xmlnodecollection을 반복하고 Google지도 마커에 대한 값을 가져 오려고합니다. 사전으로 시도하고 있지만 컬렉션의 여러 노드에서 실제로 작동하지 않습니다. 동일한 키에 대해 하나 이상의 키, 값 쌍을 저장할 수 있기를 원합니다. 누군가가 내가 어떻게 할 수 있는지 알면 더 도움이됩니다.동일한 키에 대한 C# 배열의 여러 값

 Dictionary<string, string> mapValues = new Dictionary<string, string>(); 
     foreach (XmlNode node in listProperties) 
     { 
      row = tblResults.NewRow();     
      row["Id"] = node.Attributes[0].Value;   
      row["Latitude"] = node["Location"].Attributes[0].Value; 
      row["Longitude"] = node["Location"].Attributes[1].Value; 
      row["City"] = node["Location"].Attributes[2].Value; 
      row["Address"] = node["Location"].Attributes[3].Value; 
      row["ZipCode"] = node["Location"].Attributes[4].Value; 
      row["State"] = node["Location"].Attributes[5].Value; 
      mapValues.Add("Latitude", node["Location"].Attributes[0].Value); 
      mapValues.Add("Longitude", node["Location"].Attributes[1].Value); 
      mapValues.Add("City", node["Location"].Attributes[2].Value); 
      mapValues.Add("Address", node["Location"].Attributes[3].Value); 
      mapValues.Add("ZipCode", node["Location"].Attributes[4].Value); 
      mapValues.Add("State", node["Location"].Attributes[5].Value); 

      tblResults.Rows.Add(row); 
     } 
     GenerateMap(mapValues); 

그런 다음 GenerateMap 내가 그 값을 사용하고지도 개체에 마커를 넣을 :

private void GenerateMap(Dictionary<string, string> mapInfo) 
     { 
      gMapControl1.SetCurrentPositionByKeywords("USA"); 
      gMapControl1.MinZoom = 3; 
      gMapControl1.MaxZoom = 17; 
      gMapControl1.Zoom = 4; 

      gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerAndCache; 
      gMapControl1.Position = new GMap.NET.PointLatLng(29.60862, -82.43821); 
      gMapControl1.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance; 
      GMap.NET.WindowsForms.GMapOverlay address_overlay = new GMap.NET.WindowsForms.GMapOverlay(gMapControl1, "Address1"); 

      foreach (KeyValuePair<string, string> info in mapInfo) 
      { 
       PointLatLng pnl = new PointLatLng(Convert.ToDouble(info.Value[0]), Convert.ToDouble(info.Value[1])); 
       GMapMarkerGoogleRed marker = new GMapMarkerGoogleRed(pnl); 
       MarkerTooltipMode mode = MarkerTooltipMode.OnMouseOver; 
       marker.ToolTipMode = mode; 
       marker.ToolTipText = info.Value[2] + ", " + info.Value[3] + ", " + info.Value[4] + ", " + info.Value[5]; 
       address_overlay.Markers.Add(marker); 
      } 
      gMapControl1.Overlays.Add(address_overlay); 
     } 

어떤 생각이 어떻게 그것을 달성 할 수 여기에 내가 가진 무엇인가? Windows Forms App에서이 코드를 사용하고 있습니다. 미리 감사드립니다.

+4

사용'system.linq.xml'와'ToLookup'

public class MapValues { public string Latitude { get; set; } public string Longitude{ get; set; } public string City{ get; set; } public string Address{ get; set; } public string ZipCode{ get; set; } public string State{ get; set; } public MapValues(string latitude, string longitude, string city, string address, string zipCode, string state) { this.Latitude = latitude; this.Longitude= longitude; this.City= city; this.Address= address; this.ZipCode= zipCode; this.State= state; } } 
는 다음에 코드를 변경

Jodrell

답변

0

필요한 속성을 포함하는 클래스를 만들어야하며 현재 수행중인 작업 대신 목록을 만들어야합니다. 간단하며 읽을 수 있습니다.

List<MapValues> mapValues = new List<MapValues>(); 
    foreach (XmlNode node in listProperties) 
    { 
     row = tblResults.NewRow();     
     row["Id"] = node.Attributes[0].Value;   
     row["Latitude"] = node["Location"].Attributes[0].Value; 
     row["Longitude"] = node["Location"].Attributes[1].Value; 
     row["City"] = node["Location"].Attributes[2].Value; 
     row["Address"] = node["Location"].Attributes[3].Value; 
     row["ZipCode"] = node["Location"].Attributes[4].Value; 
     row["State"] = node["Location"].Attributes[5].Value; 

     mapValues.Add(
      new MapValues(
       row["Latitude"], 
       row["Longitude"], 
       row["City"], 
       row["Address"], 
       row["ZipCode"], 
       row["State"])); 

     tblResults.Rows.Add(row); 
    } 
    GenerateMap(mapValues); 

업데이트 된 방법 :

private void GenerateMap(List<MapValues> mapInfo) 
    { 
     gMapControl1.SetCurrentPositionByKeywords("USA"); 
     gMapControl1.MinZoom = 3; 
     gMapControl1.MaxZoom = 17; 
     gMapControl1.Zoom = 4; 

     gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerAndCache; 
     gMapControl1.Position = new GMap.NET.PointLatLng(29.60862, -82.43821); 
     gMapControl1.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance; 
     GMap.NET.WindowsForms.GMapOverlay address_overlay = new GMap.NET.WindowsForms.GMapOverlay(gMapControl1, "Address1"); 

     foreach (MapValues info in mapInfo) 
     { 
      PointLatLng pnl = new PointLatLng(Convert.ToDouble(info.Latitude), Convert.ToDouble(info.Longitude)); 
      GMapMarkerGoogleRed marker = new GMapMarkerGoogleRed(pnl); 
      MarkerTooltipMode mode = MarkerTooltipMode.OnMouseOver; 
      marker.ToolTipMode = mode; 
      marker.ToolTipText = info.City + ", " + info.Address + ", " + info.ZipCode + ", " + info.State; 
      address_overlay.Markers.Add(marker); 
     } 
     gMapControl1.Overlays.Add(address_overlay); 
    } 
관련 문제