473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parsing XML with LINQ To XML and VB.NET

PsychoCoder
465 Expert Mod 256MB
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
1 2258
PsychoCoder
465 Expert Mod 256MB
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

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

Similar topics

28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
6
by: Plissskin | last post by:
I need to create an "ad-hoc" filtering page in a web app we are building. The page would display a number of drop down lists, text boxes, and radio lists and allow the user to select (enter) some...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
13
by: raylopez99 | last post by:
Refer to Int32.TryParse method: http://msdn.microsoft.com/en-us/library/zf50za27.aspx if you have to, specifically this example: numericString = "10345.72" styles = NumberStyles.Integer Or...
1
by: shapper | last post by:
Hello, I am parsing a CSV string as follows: var answers = CSVAnswers.Split(new char { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(a =a.Trim()).ToList(); I would like to create a...
1
by: Stuart Shay | last post by:
Hello All: I am working on parsing the Amazon Key word search webservice for the Editorial Reviews Content containing "Amazon.Com Review" in the Source element. <EditorialReviews>...
0
by: balint kardos | last post by:
Hi all, My question is about parsing and saving a LINQ query from/to xml. I'm creating an web interface where a user can select fields from dataset tables, and create relations between them...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.