C# 如何使用字符串拆分将其转换为Lat、Long和Alt?
我目前正在使用下面的代码解析一个XML文件以获取坐标值C# 如何使用字符串拆分将其转换为Lat、Long和Alt?,c#,silverlight,C#,Silverlight,我目前正在使用下面的代码解析一个XML文件以获取坐标值 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { WebClient busStops = new WebClient(); busStops.DownloadStringCompleted += new DownloadStringCompletedEventHandle
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient busStops = new WebClient();
busStops.DownloadStringCompleted += new DownloadStringCompletedEventHandler(busStops_DownloadStringCompleted);
busStops.DownloadStringAsync(new Uri("http://www.location.com/file.xml"));
}
void busStops_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
var busStopInfo = XDocument.Load("Content/BusStops2.xml");
var Transitresults = from root in busStopInfo.Descendants("Placemark")
let StoplocationE1 = root.Element("Point").Element("coordinates")
let nameE1 = root.Element("name")
select new TansitVariables
{
Stoplocation = StoplocationE1 == null ? null : StoplocationE1.Value,
name = nameE1 == null ? null : nameE1.Value,
};
listBox2.ItemsSource = Transitresults;
}
public class TansitVariables
{
public string Stoplocation { get; set; }
public string name { get; set; }
}
}
}
该值位于字符串StopLocation中,但我想将其转换为3个值Lat、Long和Alt
我以前没有使用过stringsplit,文档也没有解释如何从解析的输出中执行此操作
这是174.732224,-36.931053,0.000000
提前感谢。给定此输出字符串,您可以通过以下方式获得三个数字:
string result = "174.732224,-36.931053,0.000000";
var items = result.Split(',');
double longitude = double.Parse(items[0]);
double latitude = double.Parse(items[1]);
double altitude = double.Parse(items[2]);
编辑: 需要更改的代码部分可能是:
var Transitresults = from root in busStopInfo.Descendants("Placemark")
let StoplocationE1 = root.Element("Point").Element("coordinates")
let nameE1 = root.Element("name")
select new TansitVariables(
StoplocationE1 == null ? null : StoplocationE1.Value,
nameE1 == null ? null : nameE1.Value);
listBox2.ItemsSource = Transitresults;
}
// Add properties to your class
public class TransitVariables
{
// Add a constructor:
public TransitVariables(string stopLocation, string name)
{
this.StopLocation = stopLocation;
this.Name = name;
if (!string.IsNullOrWhiteSpace(stopLocation))
{
var items = stopLocation.Split(',');
this.Lon = double.Parse(items[0]);
this.Lat = double.Parse(items[1]);
this.Alt = double.Parse(items[2]);
}
}
public string StopLocation { get; set; }
public string Name { get; set; }
public double Lat { get; set; }
public double Lon { get; set; }
public double Alt { get; set; }
}
感谢Reed,问题是有多个停止位置,我需要将它们全部转换为Bing图钉Location@Rhys:上面的代码应该可以工作-只需创建一个进行此转换的方法,并在查询中调用它来设置值…我不确定如何将其实现到我的代码中。你有没有可能编辑我的代码并给我看?很抱歉,这让我很痛苦,我已经尝试了几个星期了,但由于我对C#知识的缺乏,我感到很有限。这太棒了,非常感谢。