Using For Each loop with a Where Clause for XElement
I am tring to write a for each loop that loops through the descendants of an xml doc but only the ones that satisfy a criteria.
I thought i would be able to do this with the where clause but i am having problems with this. It would be great if someone could let me know if this is actually possible or if there is another way i could do this.
I am coding in VB.Net My code so far is:
For Each Room In xmlDoc.Descendants("Rooms").Where((Room >= Room.Descendants ("ReservationID").Value = 80154))
Next
The xml that i would like to loop through is:
- <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</IsCommissiona开发者_运维问答bleBooking>
<RoomID>800009</RoomID>
<RoomNumber>Motel</RoomNumber>
</Rooms>
Thanks
It looks like you're trying to use a C# lambda =>
instead of the VB.NET Function
syntax. Refer to Lambda Expressions (Visual Basic) for more details on syntax.
Try this code instead:
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
精彩评论