我可以使用此代码
阅读并获取KML数据Placemark值using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> placemarks = doc.Descendants().Where(x => x.Name.LocalName == "Placemark").ToList();
List<string> states = placemarks.Descendants().Where(x => x.Name.LocalName == "name").Select(x => x.Value).ToList();
}
}
}
但如果它有以下详细信息,我不知道如何获取KML数据:
示例:阿拉巴马州KML格式,如
<Polygon> <outerBoundaryIs> <LinearRing> <coordinates>-85.070067,31.980703,0.0 -85.11515,31.907425,0.0 -85.135567,31.854884,0.0 -85.0567,32.017377,0.0 -85.070067,31.980703,0.0</coordinates> </LinearRing> </outerBoundaryIs> </Polygon>
示例:德克萨斯州KML格式,如
<MultiGeometry> <Polygon> <outerBoundaryIs> <LinearRing> <coordinates>-161.333785,58.703653</coordinates> </LinearRing> </outerBoundaryIs> </Polygon> <Polygon> <outerBoundaryIs> <LinearRing> <coordinates>-131.594177,55.106316,55.23548,0.0</coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </MultiGeometry>
我尝试了这个LINQ
过程来获取Polygon值:
List<string> states = placemarks.Descendants().Where(x => x.Name.LocalName == "Polygon").Select(x => x.Value).ToList();
输出:
-85.070067,31.980703,0.0 -85.11515,31.907425,0.0 -85.135567,31.854884,0.0 -85.0567,32.017377,0.0 -85.070067,31.980703,0.0
在我尝试用下面显示的格式替换坐标并将它们复制/粘贴到融合表位置预览中后,地图的工作方式完全符合预期。
<Polygon> <outerBoundaryIs> <LinearRing> <coordinates>-85.070067,31.980703,0.0 -85.11515,31.907425,0.0 -85.135567,31.854884,0.0 -85.0567,32.017377,0.0 -85.070067,31.980703,0.0</coordinates> </LinearRing> </outerBoundaryIs> </Polygon>
但是当我尝试使用相同的方式获得Multigeometry值时:
List<string> states = placemarks.Descendants().Where(x => x.Name.LocalName == "MultiGeometry").Select(x => x.Value).ToList();
我得到了这个输出:
-161.333785,58.703653 -131.594177,55.106316,55.23548,0.0
但我不知道如何获取细节。
答案 0 :(得分:0)
&#34; 在我尝试用下面显示的格式替换坐标并将它们复制/粘贴到融合表位置预览后,地图的工作方式完全符合预期。&#34;
<Polygon> <outerBoundaryIs> <LinearRing> <coordinates>-85.070067,31.980703,0.0 -85.11515,31.907425,0.0 -85.135567,31.854884,0.0 -85.0567,32.017377,0.0 -85.070067,31.980703,0.0</coordinates> </LinearRing> </outerBoundaryIs> </Polygon>
如果您想要XElement
的XML标记,只需调用ToString()
而不是获取Value
属性:
List<string> states = placemarks.Descendants()
.Where(x => x.Name.LocalName == "MultiGeometry")
.Select(x => x.ToString())
.ToList();
..或者如果你真的打算从MultiGeometry
得到个别坐标值:
List<string> states = placemarks.Descendants()
.Where(x => x.Name.LocalName == "MultiGeometry")
.Descendants()
.Where(x => x.Name.LocalName == "Polygon")
.Select(x => x.Value)
.ToList();