2016-06-13 8 views
2

私はウェブサイトtutorialからVisual Studio 2015 c#(コードにいくつかの変更を加えて)を使用して別のソリューションを作成しています。テキストファイルの値をビジュアルスタジオのC#に割り当てる方法は?

XAMLファイル:

<Window x:Class="WPFTestApplication.InsertPushpin" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" 
    Width="1024" Height="768"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <m:Map CredentialsProvider="INSERT_YOUR_BING_MAPS_KEY"> 
     </m:Map> 
    </Grid> 
    </Window> 

xaml.csファイルは次のとおりです。

1.234 
145.765 
1.267 
145.957 

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Globalization; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using Microsoft.Maps.MapControl.WPF; 
using Microsoft.Maps.MapControl.WPF.Design; 

namespace WPFTestApplication 
{ 
public partial class AddPushpinToMap : Window 
{ 
    LocationConverter locConverter = new LocationConverter(); 

public AddPushpinToMap() 
{ 
    InitializeComponent(); 
    Pushpin pin = new Pushpin(); 
    pin.Location = new Location(37.1481402218342, -119.644248783588); 

    // Adds the pushpin to the map. 
    myMap.Children.Add(pin); 

    } 
} 
} 

私はこの形式で浮動小数点値を含むテキストファイルを持っています

最初の値は緯度で、2番目の値は経度です。 これは、などの第三及び第四、第五及び第六のために繰り返される

私はコード

 pin.Location = new Location(1st_value,2nd_value); 

の行にテキストファイルから1番目と2番目の値を割り当てると、それはに画鋲を追加します地図。

しかし、私は初心者で、テキストファイルから読み込んでそのコード行に値を追加する方法がわかりません。

テキストファイルの値をコード行に割り当てるにはどうすればよいですか?

おかげ

+0

もう1つの奇妙な世界の質問。 :) –

+0

あなたの返信ありがとう:)私のコードは今働いています。 – Gajesh

答えて

4

あなたはファイルの内容を読み取るためにFile.ReadLinesメソッドを使用することができます。

初心者であれば、foreachを使用して繰り返しリストを開始できます。

var lines = File.ReadLines(filepath).ToList(); 
var locations = new List<Location>(); 
if(lines.Count() %2 !=0) throw new ArgumentException("invalid no.of vertices"); 

for(int i=0;i<lines.Count();i+=2) 
{ 
    double lat = double.Parse(lines[i]); 
    double lon = double.Parse(lines[i+1]); 

    locations.Add(new Location(lat, lon)); 
} 

あなたは以下のようにLinqでこれを行うことができLinqに精通している場合。あなたはファイルの内容を読んでたら

var locations = File.ReadLines(filepath) 
    .Select((line,i)=> new {line, index=i/2 }) 
    .GroupBy(x=>x.index) 
    .Select(x=> new Location(double.Parse(x.First().line),double.Parse(x.Last().line))) 
    .ToList(); 
1

これはあなたを助けることがあります。ファイルからすべての行(Arrayなど)を取得する 使用File.ReadAllLinesを。あなたの入力仕様に従って、latitudeはファーストラインにあり、longitudeは2番目のラインにあるので、それらのインデックスにアクセスすることができます。 double.TryParse()メソッドを使用して、これらの値を2倍に変換します。今、このために以下のコードを考えてみます。

string textF[email protected]"local path to the file"; 
var Lines= System.IO.File.ReadAllLines(textFilePath); 
double latitude,longitude; 
double.TryParse(Lines[0],out latitude); 
double.TryParse(Lines[1],out longitude); 
pin.Location = new Location(latitude,longitude); 
2

これはあなたに開始するための何かを与える必要があり、

 using (StreamReader reader = new StreamReader("*** your filepath ***")) 
     { 
      while (!reader.EndOfStream) 
      { 
       double lat = double.Parse(reader.ReadLine()); 
       double lon = double.Parse(reader.ReadLine()); 

       pin.Location = new Location(lat, lon); 
      } 
     } 
1

、あなたはList内のすべての緯度と経度情報の収集を維持することができ、各リスト項目は、緯度と経度の値のペアになります。 Tupleはここで目的を解決する必要があります。

var latitude = latLongInfoList.First().Item1; 
var longitude = latLongInfoList.First().Item2; 
pin.Location = new Location(latitude,longitude); 

、コーナーケースをチェックし、それに応じてそれらを扱う、ラインは2の乗数ではない何かのようでください - あなたはその後、例えば、次のようにコレクション内のデータを使用することができます

private void BuildGeoInfo() 
    { 
     string textFilePath = @"path to your text file"; 

     //Read all the contents of file as list of lines 
     var fileLines = System.IO.File.ReadAllLines(textFilePath).ToList(); 

     //This list will hold the Latitude and Longitude information in pairs 
     List<Tuple<double, double>> latLongInfoList = new List<Tuple<double, double>>(); 

     int index = 0; 
     while (index < fileLines.Count) 
     { 
      var latLongInfo = new Tuple<double, double>(
            Convert.ToDouble(fileLines[index]), 
            //++ to index to get value of next line 
            Convert.ToDouble(fileLines[index++])); 

      latLongInfoList.Add(latLongInfo); 

      index++; //++ to index to move to next line 
     } 
    } 

各テキスト行の種類など

関連する問題