使用VB.net轴运算符(... /。/ @)如何选择/迭代具有特定子字符串的节点。
E.g。
采用这样的XML代码段:
<Route name="Fly from London Heathrow" distance="1192.88" duration="355">
<IndicativePrice price="163" currency="USD" isFreeTransfer="0"/>
<Stop name="Surbiton" pos="51.39301,-0.30677" kind="town"/>
<Stop name="Kingston Upon Thames, Kingston" pos="51.41182,-0.3006" kind="station"/>
<Stop name="London Heathrow" pos="51.47034,-0.45342" kind="airport" code="LHR"/>
<Stop name="Barcelona" pos="41.30373,2.07315" kind="airport" code="BCN"/>
<Stop name="Barcelona" pos="41.38702,2.17004" kind="city"/>
[SO-point 1]
<TransitSegment kind="bus" subkind="bus" vehicle="bus" isMajor="0" isImperial="1" ..."></TransitSegment>
<TransitSegment kind="bus" subkind="bus" vehicle="bus" isMajor="0" isImperial="1" distance="18.4 ..."></TransitSegment>
<FlightSegment kind="flight" isMajor="1" distance="1146.72" duration="120" sCode="LHR" tCode="BCN"></FlightSegment>
<TransitSegment kind="train" subkind="train" vehicle="train" isMajor="0" isImperial="0" distance="25.07" duration="25" sName="Aeropuerto de Prat" sPos="41.30426,2.07352" sCode="7172400" tName="Barcelona P.Gracia" tPos="41.38725,2.16866" tCode="7100063" path=".."></TransitSegment>
[SO-point 2]
</Route>
我可以像这样迭代Stops:
For Each routeStop In route.<ns:Stop>
Dim nextStop As New r2rSearchResponseRoutes.r2rSearchResponseStops
nextStop.kind = routeStop.@kind
nextStop.name = routeStop.@name
nextStop.code = routeStop.@code
nextStop.pos = routeStop.@pos
nextRoute.listOfStops.Add(nextStop)
Next
如何针对具有子字符串的任何节点进行选择,例如节点名称中的“Segment”如上例所示?如上所示[SO-point 1]至[SO-point 2]
答案 0 :(得分:0)
您需要使用LINQ方法来过滤XML节点,例如:
For Each s As XElement In route.Elements().Where(Function(x) x.Name.LocalName.Contains("Segment"))
Console.WriteLine(s.ToString())
Next