好的,所以我一直在努力读取kml文件,该文件包含美国每个县/市边界的坐标。但是,我遇到了一些问题。特别是,如何获取NextNode的值以及在元素值中间存在另一个元素标签时该怎么做。
这是kml文件的样子:
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="gadm36_USA_2" id="gadm36_USA_2">
<SimpleField name="NAME_0" type="string"></SimpleField>
<SimpleField name="NAME_1" type="string"></SimpleField>
<SimpleField name="NAME_2" type="string"></SimpleField>
</Schema>
<Folder><name>gadm36_USA_2</name>
<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#gadm36_USA_2">
<SimpleData name="NAME_0">United States</SimpleData>
<SimpleData name="NAME_1">Alabama</SimpleData>
<SimpleData name="NAME_2">Autauga</SimpleData>
</SchemaData></ExtendedData>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>-86.8189620971679,32.3402709960939 -86.8108367919922,32.3471298217775 -86.8097915649414,32.3535118103028 -86.8103485107422,32.3585205078126 -86.8158340454101,32.3703498840333 -86.8239974975586,32.3785285949708 -86.8310775756835,32.3839797973634 -86.83544921875,32.3912506103515 -86.8419876098633,32.3980712890626 -86.8452758789062,32.4044418334961 -86.8458633422851,32.4140090942383 -86.8447875976562,32.4167404174805 </coordinates></LinearRing></outerBoundaryIs></Polygon><Polygon><outerBoundaryIs><LinearRing><coordinates>-86.8426208496093,32.4181213378907 -86.8361129760742,32.4204101562501 -86.8296127319336,32.4227104187012 -86.8274383544922,32.4240798950195 -86.8263626098633,32.4259109497071 -86.8280029296875,32.4277305603028 -86.8307189941406,32.4295387268066 </coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
</Placemark>
请注意,这不是一个实际示例,这些随机元素标签出现在坐标的中间,拥有它们的县通常具有庞大的坐标列表,根据我的经验,如果我浏览kml文件并仅使用第一个元素标签之前的值,它似乎可以绘制出正确的县边界
List<string> locationList = new List<string>();
var doc = XDocument.Load("gadm36_USA1.kml");
XNamespace ns = "http://www.opengis.net/kml/2.2";
var result = doc.Root.Descendants(ns + "Placemark");
foreach (XElement xmlInfo in result)
{
var region = xmlInfo.Element(ns + "ExtendedData").Element(ns + "SchemaData").Value;
//var country = region.Element(ns + "SimpleData").Value;
//var state = region.Element(ns + "SimpleData");
//var cityCounty = region.Element(ns + "SimpleData");
locationList = xmlInfo.Element(ns + "MultiGeometry").Value.Split(' ').ToList();
CountyCoordinates.Add(region, locationList);
}
因此,当我到达变量“ region”时,它将所有元素值组合在一起。例如,它将说“美国Autauga阿拉巴马州”。
对于坐标,由于在坐标值的中间有这些随机元素标签,所以当我将坐标除以“”时,碰到这些随机元素标签时会搞砸。 (当到达坐标中间的文本时,拆分将返回“ -86,32 -86”,而不只是“ -86,32”。)因此,我实质上是在寻求有关如何读入文本的帮助国家,州和县,以及尽管有这些随机元素标签,如何正确读取坐标。
答案 0 :(得分:0)
我刚刚测试了以下结果:
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);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
List<XElement> simpleFields;
List<XElement> extendedDatas = doc.Descendants(ns + "ExtendedData").ToList();
foreach(XElement extendedData in extendedDatas)
{
simpleFields = extendedData.Descendants(ns + "SimpleData").ToList();
}
}
}
}
答案 1 :(得分:0)
我想你是说:
List<XElement> extendedDatas = doc.Descendants(ns + "ExtendedData").ToList();
var a_docment = root.Element(ns + "Document").Value;
var result = doc.Root.Descendants(ns + "Placemark");
foreach (XElement xmlInfo in result)
{
List<string> locationList = new List<string>();
var gg = xmlInfo.Descendants(ns + "outerBoundaryIs").ToList();
foreach (XElement extendedData in gg)
{
locationList.AddRange(extendedData.Value.Split(' ').ToList());
}