2014-04-17 56 views
0

我目前的任務是在Visual C#2010 Express中創建一個小型C#應用程序,該應用程序加載CSV文件並處理信息以創建要在地圖上顯示的點數組。是 在CSV的條目如下:加載CSV數據並創建點陣

設備;緯度;經度;速度;時間

57EA7531-0E1F-41C7-B785-22398D445FEA; 55.512.653; 13.306.292; 93; 13-4 -2014 14:01

的想法是加載該信息,分割數據,它指定爲下面的代碼的不同屬性:

ShapeLayer sl = new ShapeLayer("Marker"); 
wpfMap.Layers.Add(sl); 
marker.Width = marker.Height = 20; 
marker.ToolTip = "A map marker"; [needs to contain Device;Latitude;Longitude;Speed;Time] 
sl.Shapes.Add(marker); 
ShapeCanvas.SetLocation(marker, new System.Windows.Point(8.4, 49)); [needs to contain Longitude,Latitude] 

指出其間[]是從需要的CSV數據被輸入。

此CSV文件包含約2000個條目,並且對於每個條目,需要使用上述代碼創建點。我在加載CSV和使用處理後的數據創建陣列方面的經驗非常有限,是否有任何幫助我的方法?

+0

看看http://stackoverflow.com/questions/1375410/very-simple-c-sharp-csv-reader加載CSV和分割數據 –

+0

至於你提到CSV包含2000個條目。匹配哪個條目屬於哪個標記的標準是什麼? – Hassan

+0

CSV文件每行中的數據應該在該段代碼中處理並存儲在「標記」數組中,之後我可以顯示它們。 – KDE

回答

0

有不同的方法。我會做那樣的事情。

備註。此解決方案需要異常處理。

string[] allLines = System.IO.File.ReadAllLines(@"yourCVSPath.csv"); 

foreach(string sLine in allLines) 
{ 
     string[] arrLine = sLine.Split(new char[] { ',' }); // or ';' according to provided data 

      if (arrLine.Length == 5) 
      { 
       string sDevice = arrLine[0]; 
       string sLatitude = arrLine[1]; 
       string sLongitude = arrLine[2]; 
       string sSpeed = arrLine[3]; 
       string sTime = arrLine[4]; 

       ShapeLayer sl = new ShapeLayer("Marker"); 
       wpfMap.Layers.Add(sl); 
       marker.Width = marker.Height = 20; 
       marker.ToolTip = sLine; //[needs to contain Device;Latitude;Longitude;Speed;Time] 
       sl.Shapes.Add(marker); 
       ShapeCanvas.SetLocation(marker, new System.Windows.Point(sLongitude, sLatitude)); //[needs to contain Longitude,Latitude] 
      } 
    }