Reputation: 10765
I am attempting to parse an XML feed from a web service with LINQ To XML (My first attempt at it so bear with me). I ave the following function which is supposed to create me an IEnumerable(Of RoomRate) from the XML
Public Function ParseRates() As IEnumerable(Of RoomRate)
Try
Return From el As XElement In _xDoc...<RoomRate>
Select New RoomRate With { _
.GuaranteeSurchargeRequired = el.@GuaranteeSurchargeRequired, _
.IATACharacteristicIdentification = el.@IATACharacteristicIdentification, _
.IATAProductIdentification = el.@IATAProductIdentification, _
.RPH = el.@RPH, _
.CancellationPolicy = el...<AdditionalInfo>...<CancellationPolicy>.@Numeric, _
.Commission = el...<AdditionalInfo>...<Commission>.@NonCommission, _
.Rate = el...<Rates>...<Rate>.@Amount, _
.CurrencyCode = el...<Rates>...<Rate>.@CurrencyCode, _
.TotalPrice = el...<Rates>...<Rate>...<HotelTotalPricing>.@Amount, _
.Surcharge = el...<Rates>...<Rate>...<TotalSurchjarges>.@Amount}
Catch ex As Exception
ErrorMessage = ex.Message
Return Nothing
End Try
End Function
And I'm testing it like so
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim parse As New ParseSabre(Server.MapPath("~/XML/SabreSample.xml"))
Dim rates As IEnumerable(Of RoomRate)()
rates = parse.ParseRates()
'If rates = Nothing Then
'End If
For Each rate As RoomRate In rates
Response.Write(rate.GuaranteeSurchargeRequired)
Response.Write(rate.IATACharacteristicIdentification)
Response.Write(rate.IATAProductIdentification)
Response.Write(rate.RPH)
Response.Write(rate.CancellationPolicy)
Response.Write(rate.Commission)
Response.Write(rate.Rate)
Response.Write(rate.CurrencyCode)
Response.Write(rate.TotalPrice)
Response.Write(rate.Surcharge)
Response.Write(vbNewLine)
Next
End Sub
But on this line
rates = parse.ParseRates()
I'm getting this error:
Unable to cast object of type 'WhereSelectEnumerableIterator
2[System.Xml.Linq.XElement,Lodgx.Classes.Models.RoomRate]' to type 'System.Collections.Generic.IEnumerable
1[Lodgx.Classes.Models.RoomRate][]'.
Can someone help me figure out whjere I'm going wrong here?
EDIT
I've changed my code to this, is this better? (Also, still getting same error with this code)
_rates = _xDoc.Descendants("RoomRate").Select(Function(n) New RoomRate With { _
.GuaranteeSurchargeRequired = n.Attribute("GuarateeSurchargeRequired").Value, _
.IATACharacteristicIdentification = n.Attribute("IATACharacteristicIdentification").Value, _
.IATAProductIdentification = n.Attribute("IATAProductIdentification").Value,
.RPH = n.Attribute("RPH").Value})
Upvotes: 0
Views: 150
Reputation: 125650
Problem is, your declaring your variable as an array of IEnumerable(Of RoomRate)
Dim rates As IEnumerable(Of RoomRate)()
(important part is ()
after type name - that's how you initialize array in VB.NET) and trying to assign only one IEnumerable(Of RoomRate)
instance to it
' ParseRates() returns IEnumerable(Of RoomRate) '
rates = parse.ParseRates()
Looking at your code I assume that you actually need and want only one collection, you should change rates
variable declaration to:
Dim rates As IEnumerable(Of RoomRate)
Upvotes: 1
Reputation: 5122
I'm not too familiar with LINQ and reading XML but I believe the error you are getting is caused by the casting of the Select result and IEnumerable(Of RoomRate) because they don't match. Instead you can try .ToList() on the select and have your rates variable of type List(Of RoomRate).
Dim rates as List(Of RoomRate) = _xDoc.Descendants("RoomRate").Select(Function(n) New RoomRate With { _
.GuaranteeSurchargeRequired = n.Attribute("GuarateeSurchargeRequired").Value, _
.IATACharacteristicIdentification = n.Attribute("IATACharacteristicIdentification").Value, _
.IATAProductIdentification = n.Attribute("IATAProductIdentification").Value,
.RPH = n.Attribute("RPH").Value}).ToList()
Upvotes: 1