使用For Each循环和XElement的Where子句

时间:2010-10-05 20:43:35

标签: vb.net xelement

我想为每个循环编写一个循环遍历xml文档后代的循环,但只有符合条件的循环。

我以为我可以用where子句做到这一点,但我遇到了这个问题。如果有人能告诉我这是否真的有可能,或者我还有其他办法可以做到这一点,那就太好了。

我在VB.Net编码我的代码到目前为止是:

For Each Room In xmlDoc.Descendants("Rooms").Where((Room >= Room.Descendants    ("ReservationID").Value = 80154))

 Next

我想循环的xml是:

- <NewDataSet xmlns="">
- <Rooms diffgr:id="Rooms1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
  <ReservationID>80154</ReservationID> 
  <Ref /> 
  <ReservationDateTime>5/10/2010 1:35:27 p.m.</ReservationDateTime> 
  <IsCommissionableBooking>False</IsCommissionableBooking> 
  <RoomID>800010</RoomID>  
  <RoomNumber>Double</RoomNumber> 
  </Rooms>
- <Rooms diffgr:id="Rooms2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
  <ReservationID>80154</ReservationID> 
  <Ref /> 
  <ReservationDateTime>5/10/2010 1:35:27 p.m.</ReservationDateTime> 
  <IsCommissionableBooking>False</IsCommissionableBooking> 
  <RoomID>800009</RoomID> 
  <RoomNumber>Motel</RoomNumber> 
  </Rooms>
- <Rooms diffgr:id="Rooms3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
  <ReservationID>80151</ReservationID> 
  <Ref /> 
  <ReservationDateTime>5/10/2010 12:22:16 p.m.</ReservationDateTime> 
  <IsCommissionableBooking>False</IsCommissionableBooking> 
  <RoomID>800009</RoomID> 
  <RoomNumber>Motel</RoomNumber> 
</Rooms>

由于

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试使用C#lambda =>而不是VB.NET Function语法。有关语法的详细信息,请参阅Lambda Expressions (Visual Basic)

请尝试使用此代码:

Dim query = xml.Descendants("Rooms").Where(Function(r) r.Descendants("ReservationID").Value = 80154)
For Each room In query
    ' do something with room '
    Console.WriteLine("Room ID: " & room.Element("RoomID").Value)
Next