我可以读取和获取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:temptest.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
过程来获得多边形值:
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
但我不知道如何获得详细信息
"我试着用下面的格式替换坐标,并将它们复制/粘贴到融合表位置预览中,地图的效果完全符合预期。"
<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();