我一直在努力编写一些代码,这些代码将从包含Google地球多边形的kml中读取数据并提取名称和坐标,并将所有内容存储为纬度和经度。 我已经创建了一个用户表单,允许用户浏览kml,然后运行提取代码。不幸的是,提取不起作用。 我对VB很陌生,但我确实在大学里学了三个学期的C ++,但是从那时起已经接近一年了。 这就是我所拥有的,但我明白我也可能完全错了..
Function X(InputFile As String, Text As String)
Dim textReader As New Xml.XmlTextReader(InputFile)
Dim lastElementName As String = ""
While textReader.Read()
Select Case textReader.NodeType
Case Xml.XmlNodeType.Element
lastElementName = textReader.Name
Case Xml.XmlNodeType.Text
MsgBox(lastElementName & ": " & textReader.Value)
End Select
Console.WriteLine()
End While
基本KML示例:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>The Pentagon</name>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-77.05668055019126,38.87154239798456
-77.05542625960818,38.87167890344077
-77.05485125901024,38.87076535397792
-77.05577677433152,38.87008686581446
-77.05691162017543,38.87054446963351
-77.05668055019126,38.87154239798456
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</kml>
答案 0 :(得分:1)
据我所知,您有以下子问题:
解析XML并提取名称和坐标。
将坐标拆分为某种数据结构。
对于第1步,VB的LINQ to XML是最简单的方法。以下工作代码示例应该可以帮助您入门:
Imports <xmlns="http://www.opengis.net/kml/2.2">
Module Module1
Sub Main()
Dim xdata = New XDocument( _
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>The Pentagon</name>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-77.05668055019126,38.87154239798456
-77.05542625960818,38.87167890344077
-77.05485125901024,38.87076535397792
-77.05577677433152,38.87008686581446
-77.05691162017543,38.87054446963351
-77.05668055019126,38.87154239798456
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</kml>)
For Each p In xdata.Root.<Placemark>
Console.WriteLine("Name: " & p.<name>.Value)
For Each c In p...<coordinates>
Console.WriteLine("Coordinates: " & c.Value)
Next
Next
Console.ReadLine()
End Sub
End Module
一些评论:
XDocument.Load
代替New XDocument
。Imports
statement at the top of the file很重要,它必须与KML文件开头的xmlns声明匹配。第2步仍然是练习,但是String.Split
(第一次在线休息,然后在逗号上)和String.Trim
(删除空格)应该可以让你轻松解决这个问题。