By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Parsing XML with LINQ To XML and VB.NET

PsychoCoder
Expert Mod 100+
P: 465
I'm working on parsing XML returned from a web service call. This is a sample of the returned XML (the actually result is much larger):

Expand|Select|Wrap|Line Numbers
  1. ?xml version="1.0" encoding="utf-16"?>
  2. <HotelPropertyDescriptionRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" TimeStamp="2013-12-30T18:49:36" Version="1.14.1">
  3.   <Success xmlns="http://webservices.sabre.com/sabreXML/2003/07" />
  4.   <RoomStay xmlns="http://webservices.sabre.com/sabreXML/2003/07">
  5.     <RoomRates>
  6.       <RoomRate GuaranteeSurchargeRequired="G" IATACharacteristicIdentification="BGGO00" IATAProductIdentification="BLOOMBERG" RPH="001">
  7.         <AdditionalInfo>
  8.           <CancelPolicy Numeric="06" />
  9.           <Commission NonCommission="true">NON COMMISSIONABLE</Commission>
  10.           <Text>BLOOMBERG LP, 0.0 KM, INCLUDES BREAKFAST, INTERNET, WIFI, SEE</Text>
  11.           <Text>RATE RULES DELUXE ROOM, GUEST ROOM, 1 KING OR 2 TWIN/SINGLE BE</Text>
  12.         </AdditionalInfo>
  13.         <Rates>
  14.           <Rate Amount="66.600" CurrencyCode="KWD">
  15.             <AdditionalGuestAmounts>
  16.               <AdditionalGuestAmount MaxExtraPersonsAllowed="0">
  17.                 <Charges ExtraPerson="0" />
  18.               </AdditionalGuestAmount>
  19.             </AdditionalGuestAmounts>
  20.             <HotelTotalPricing Amount="76.590">
  21.               <Disclaimer>INCLUDES TAXES AND SURCHARGES</Disclaimer>
  22.               <TotalSurcharges Amount="9.990" />
  23.             </HotelTotalPricing>
  24.           </Rate>
  25.         </Rates>
  26.       </RoomRate>
  27. ...
I am attempting to parse it into this class

Expand|Select|Wrap|Line Numbers
  1. Namespace Classes.Models
  2.     Public Class RoomRate
  3.         Public Property Rate() As String
  4.         Public Property Surcharge() As String
  5.         Public Property TotalPrice() As String
  6.         Public Property CurrencyCode() As String
  7.         Public Property CancellationPolicy() As String
  8.         Public Property Disclaimer() As String
  9.         Public Property Text() As List(Of String)
  10.         Public Property Commission() As String
  11.         Public Property GuaranteeSurchargeRequired() As String
  12.         Public Property IATACharacteristicIdentification() As String
  13.         Public Property IATAProductIdentification() As String
  14.         Public Property RPH() As String
  15.     End Class
  16. End Namespace
I have this function which should accomplish this:

Expand|Select|Wrap|Line Numbers
  1. Public Function ParseRates() As IEnumerable(Of RoomRate)
  2.     Try
  3.         Return From el As XElement In _xDoc...<RoomRate>
  4.                     Select New RoomRate With { _
  5.                        .GuaranteeSurchargeRequired = el.@GuaranteeSurchargeRequired, _
  6.                        .IATACharacteristicIdentification = el.@IATACharacteristicIdentification, _
  7.                        .IATAProductIdentification = el.@IATAProductIdentification, _
  8.                        .RPH = el.@RPH, _
  9.                        .CancellationPolicy = el...<AdditionalInfo>...<CancellationPolicy>.@Numeric, _
  10.                        .Commission = el...<AdditionalInfo>...<Commission>.@NonCommission, _
  11.                        .Rate = el...<Rates>...<Rate>.@Amount, _
  12.                        .CurrencyCode = el...<Rates>...<Rate>.@CurrencyCode, _
  13.                        .TotalPrice = el...<Rates>...<Rate>...<HotelTotalPricing>.@Amount, _
  14.                        .Surcharge = el...<Rates>...<Rate>...<TotalSurchjarges>.@Amount}
  15.     Catch ex As Exception
  16.         ErrorMessage = ex.Message
  17.         Return Nothing
  18.     End Try
  19. End Function
Then when I try and test it with this test page (page load event):

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.     Dim parse As New ParseSabre(Server.MapPath("~/XML/SabreSample.xml"))
  3.     Dim rates As IEnumerable(Of RoomRate)()
  4.  
  5.     rates = parse.ParseRates()
  6.  
  7.     'If rates = Nothing Then
  8.  
  9.     'End If
  10.     For Each rate As RoomRate In rates
  11.         Response.Write(rate.GuaranteeSurchargeRequired)
  12.         Response.Write(rate.IATACharacteristicIdentification)
  13.         Response.Write(rate.IATAProductIdentification)
  14.         Response.Write(rate.RPH)
  15.         Response.Write(rate.CancellationPolicy)
  16.         Response.Write(rate.Commission)
  17.         Response.Write(rate.Rate)
  18.         Response.Write(rate.CurrencyCode)
  19.         Response.Write(rate.TotalPrice)
  20.         Response.Write(rate.Surcharge)
  21.         Response.Write(vbNewLine)
  22.     Next
  23. End Sub
I get the following error

Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,Lodgx.Classes.Models.Room Rate]' to type 'System.Collections.Generic.IEnumerable`1[Lodgx.Classes.Models.RoomRate][]'.

Can someone tell me
  1. Is my code for parsing going to accomplish what I'm looking for?
  2. How can I get rid of that error so I can continue to test this?
Feb 8 '14 #1
Share this Question
Share on Google+
1 Reply


PsychoCoder
Expert Mod 100+
P: 465
I have changed my code to look like so. I get no error but no values are returned:

Expand|Select|Wrap|Line Numbers
  1. For Each n As XElement In _xDoc.Descendants("RoomRate")
  2.     rate = New RoomRate()
  3.     rate.GuaranteeSurchargeRequired = n.Attribute("GuaranteeSurchargeRequired").Value
  4.     rate.IATACharacteristicIdentification = n.Attribute("IATACharacteristicIdentification").Value
  5.     rate.IATAProductIdentification = n.Attribute("IATAProductIdentification")
  6.  
  7.     _rates.Add(rate)
  8. Next
Feb 9 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.