我正在尝试从下面的XML中读取/列出每个<lat>
和<long>
点。
XML文档是一个大文件(大约30,000行),这是一个非常简化的版本:
Dim strMyXMLString As String = ""
strMyXMLString &= "<job>" & vbCrLf
strMyXMLString &= " <field>" & vbCrLf
strMyXMLString &= " <zones>" & vbCrLf
strMyXMLString &= " <zone>" & vbCrLf
strMyXMLString &= " <zone_id>BEBB35C8</zone_id>" & vbCrLf
strMyXMLString &= " <reference_id>1</reference_id>" & vbCrLf
strMyXMLString &= " <point>" & vbCrLf
strMyXMLString &= " <lat>54.141774116</lat>" & vbCrLf
strMyXMLString &= " <lng>-0.754275931</lng>" & vbCrLf
strMyXMLString &= " </point>" & vbCrLf
strMyXMLString &= " <point>" & vbCrLf
strMyXMLString &= " <lat>54.141170056</lat>" & vbCrLf
strMyXMLString &= " <lng>-0.754093629</lng>" & vbCrLf
strMyXMLString &= " </point>" & vbCrLf
strMyXMLString &= " <point>" & vbCrLf
strMyXMLString &= " <lat>54.141169615</lat>" & vbCrLf
strMyXMLString &= " <lng>-0.754103682</lng>" & vbCrLf
strMyXMLString &= " </point>" & vbCrLf
strMyXMLString &= " </zone>" & vbCrLf
strMyXMLString &= " <zone>" & vbCrLf
strMyXMLString &= " <zone_id>F9A7ECBB</zone_id>" & vbCrLf
strMyXMLString &= " <reference_id>2</reference_id>" & vbCrLf
strMyXMLString &= " <point>" & vbCrLf
strMyXMLString &= " <lat>54.142055217</lat>" & vbCrLf
strMyXMLString &= " <lng>-0.752604533</lng>" & vbCrLf
strMyXMLString &= " </point>" & vbCrLf
strMyXMLString &= " <point>" & vbCrLf
strMyXMLString &= " <lat>54.141530045</lat>" & vbCrLf
strMyXMLString &= " <lng>-0.752207708</lng>" & vbCrLf
strMyXMLString &= " </point>" & vbCrLf
strMyXMLString &= " <point>" & vbCrLf
strMyXMLString &= " <lat>54.141528842</lat>" & vbCrLf
strMyXMLString &= " <lng>-0.752213287</lng>" & vbCrLf
strMyXMLString &= " </point>" & vbCrLf
strMyXMLString &= " </zone>" & vbCrLf
strMyXMLString &= " </zones>" & vbCrLf
strMyXMLString &= " </field>" & vbCrLf
strMyXMLString &= "</job>" & vbCrLf
只有一个<zones>
元素,但其中包含许多<zone>
元素。
在上面的示例中,我还列出了每个元素的三个<point>
坐标,在实际的XML文件中,每个<point>
元素大约有<zone>
个值。
我需要遍历每个<zone>
元素并检索<zone_id>
,<reference_id>
以及每个<lat>
和<lng>
中包含的<zone>
和Dim strMyXMLDoc = System.Xml.Linq.XDocument.Parse(strMyXMLString)
Dim zone = From point In strMyXMLDoc...<zones>.<zone> Select point
For Each item In zone
' Output some stuff
Response.Write("<br>")
Response.Write("<br>zone_id: " & item.<zone_id>.Value)
Response.Write("<br>reference_id: " & item.<reference_id>.Value)
' This only lists the FIRST <lat> found and no more, which is technically right as we are still in the <zone> loop
Response.Write("<br>first lat: " & item.<point>.<lat>.Value)
Response.Write("<br>first lng: " & item.<point>.<lng>.Value)
' Getting desparate, try a nested loop
Dim zone_point = From nested_points In strMyXMLDoc...<zones>.<zone>.<point> Select nested_points
For Each zone_item In zone_point
Response.Write("<br>lat: " & item.<point>.<lat>.Value)
Response.Write("<br>lng: " & item.<point>.<lng>.Value)
Next
Next
zone_id: BEBB35C8
reference_id: 1
first lat: 54.141774116
first lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
zone_id: F9A7ECBB
reference_id: 2
first lat: 54.142055217
first lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
1}}。
以下代码:
<lat>
产生以下输出:
<lng>
正如您所看到的,它重复了第一次Dim strMyXMLDoc = System.Xml.Linq.XDocument.Parse(strMyXMLString)
Dim zone = From point In strMyXMLDoc...<zones>.<zone> Select point
For Each item In zone
' Output some stuff
Response.Write("<br>")
Response.Write("<br>zone_id: " & item.<zone_id>.Value)
Response.Write("<br>reference_id: " & item.<reference_id>.Value)
' This only lists the FIRST <lat> found and no more, which is technically right as we are still in the <zone> loop
Response.Write("<br>first lat: " & item.<point>.<lat>.Value)
Response.Write("<br>first lng: " & item.<point>.<lng>.Value)
' Getting desparate, try a nested loop
Dim zone_point = From nested_points In strMyXMLDoc...<zone>.<point> Select nested_points
For Each zone_item In zone_point
Response.Write("<br>lat: " & zone_item.<lat>.Value)
Response.Write("<br>lng: " & zone_item.<lng>.Value)
Next
Next
和zone
六次。
我也尝试过这个小代码更改:
zone_id: BEBB35C8
reference_id: 1
lat: 54.141774116
lng: -0.754275931
lat: 54.141170056
lng: -0.754093629
lat: 54.141169615
lng: -0.754103682
lat: 54.142055217
lng: -0.752604533
lat: 54.141530045
lng: -0.752207708
lat: 54.141528842
lng: -0.752213287
zone_id: F9A7ECBB
reference_id: 2
lat: 54.141774116
lng: -0.754275931
lat: 54.141170056
lng: -0.754093629
lat: 54.141169615
lng: -0.754103682
lat: 54.142055217
lng: -0.752604533
lat: 54.141530045
lng: -0.752207708
lat: 54.141528842
lng: -0.752213287
但是这会给出以下输出(它现在列出它找到的所有点,每zone_id: BEBB35C8
reference_id: 1
first lat: 54.141774116
first lng: -0.754275931
lat: 54.141774116
lng: -0.754275931
lat: 54.141170056
lng: -0.754093629
lat: 54.141169615
lng: -0.754103682
zone_id: F9A7ECBB
reference_id: 2
first lat: 54.142055217
first lng: -0.752604533
lat: 54.142055217
lng: -0.752604533
lat: 54.141530045
lng: -0.752207708
lat: 54.141528842
lng: -0.752213287
):
{{1}}
所以我们在那里有空白值,但它又循环了六次(而不是三次,对吗?)
我想要的输出需要是:
{{1}}
我做错了什么?
答案 0 :(得分:0)
解决!
此代码块:
Dim strMyXMLDoc = System.Xml.Linq.XDocument.Parse(strMyXMLString)
Dim zone = From point In strMyXMLDoc...<zones>.<zone> Select point
For Each item In zone
' Output some stuff
Response.Write("<br>")
Response.Write("<br>zone_id: " & item.<zone_id>.Value)
Response.Write("<br>reference_id: " & item.<reference_id>.Value)
' Nested loop
Dim nested_point = item.<point>
For Each zone_point In nested_point
Response.Write("<br>lat: " & zone_point.<lat>.Value)
Response.Write("<br>lng: " & zone_point.<lng>.Value)
Next
Next
提供输出:
zone_id: BEBB35C8
reference_id: 1
lat: 54.141774116
lng: -0.754275931
lat: 54.141170056
lng: -0.754093629
lat: 54.141169615
lng: -0.754103682
zone_id: F9A7ECBB
reference_id: 2
lat: 54.142055217
lng: -0.752604533
lat: 54.141530045
lng: -0.752207708
lat: 54.141528842
lng: -0.752213287
键 行:
Dim nested_point = item.<point>