【发布时间】:2019-09-03 13:02:32
【问题描述】:
我有一个 KML 文件,其中包含 GPS 坐标以及每个坐标对下一个坐标的方位。我想检索每个坐标和每个方位并将它们存储在单独的列表中。一个包含严格坐标,一个包含每个方位。
为了解决问题,我将仅列出文件中的 2 个 Placemark 元素。 我的 KML 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Folder>
<Placemark>
<name>Coordinate0</name>
<Point>
<coordinates>44.601788291074442,-88.048173934221268</coordinates>
</Point>
</Placemark>
<!-- more coordinates and bearings fill these gaps -->
<Placemark>
<name>Bearing200</name>
<Point>
<coordinates>284.25265584263923,0</coordinates>
</Point>
</Placemark>
</Folder>
</kml>
我尝试了以下方法:
//Function that parses through the file and pulls out coordinates and bearings for each coordinate
private void ButtonTestParser_Click(object sender, EventArgs e)
{
//load the xml file
XDocument xmlFile = XDocument.Load("Test.kml");
//get every placemark element in the document
var placemarks = (from x in xmlFile.Descendants()
where x.Name.LocalName == "Placemark"
select new XElement(x)).ToList();
//loop through each placemark and separate it into coordinates and bearings
List<string> coordinates = new List<string>();
string coordinate = "";
foreach(var point in placemarks)
{
coordinate = (from x in point.Descendants("coordinates")
select x).First().Value;
coordinates.Add(coordinate);
}
}
我收到以下错误:
System.InvalidOperationException: '序列不包含任何元素'
【问题讨论】:
标签: c# xml xml-parsing kml