Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 13, 2012 06:15 AM by Mamboking
Member
183 Points
125 Posts
Dec 11, 2012 10:05 AM|LINK
Hello!
I'm trying to iterate over value of nested nodes in xml files with such structure:
<HotelValuedAvailRS xmlns="http://www.hotelbeds.com/schemas/2005/06/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://www.hotelbeds.com/schemas/2005/06/messages HotelValuedAvailRS.xsd" timeToExpiration="1799559" totalItems="326" echoToken="DummyEchoToken"> <AuditData> <ProcessTime>1016</ProcessTime> <Timestamp>2007-01-09 16:08:56.263</Timestamp> <RequestHost>10.0.0.182</RequestHost> <ServerName>ITINTERNET08</ServerName> <ServerId>LO</ServerId> <SchemaRelease>2005/06</SchemaRelease> <HydraCoreRelease>2.0.200701021600</HydraCoreRelease> <HydraEnumerationsRelease>1.0.200701021600</HydraEnumerationsRelease> <MerlinRelease>N/A</MerlinRelease> </AuditData> <PaginationData currentPage="1" totalPages="17"/> <ServiceHotel xsi:type="ServiceHotel" availToken="gSPgFZmXOz/wKAXjNFfUdQ=="> <ContractList> <Contract> <Name>CGMVARIOS</Name> <IncomingOffice code="1"/> <Classification code="NOR">STANDARD PRICING</Classification> </Contract> </ContractList> <DateFrom date="20071022"/> <DateTo date="20071024"/> <Currency code="EUR">Euro</Currency> <HotelInfo xsi:type="ProductHotel"> <Code>76</Code> <Name>Bendinat</Name> <ImageList> <Image> <Type>JPG</Type> <Order>1</Order> <VisualizationOrder>1</VisualizationOrder> <Url>http://www.hotelbeds.com/giata/small/old/MALLORCA/BENDINAT/BENDINATHOT.JPG</Url> </Image> <Image> <Type>jpg</Type> <Order>2</Order> <VisualizationOrder>2</VisualizationOrder> <Url>http://www.hotelbeds.com/giata/small/036157/036157_hb_r_002.jpg</Url> </Image> </ImageList> <Category type="SIMPLE" code="4EST" shortname="4*">4 STARS</Category> <Destination type="SIMPLE" code="PMI"> <Name>Majorca</Name> <ZoneList> <Zone type="SIMPLE" code="25">Bendinat</Zone> </ZoneList> </Destination> <Position latitude="39.53240000000000264890" longitude="2.58029999999999981597"/> </HotelInfo> <AvailableRoom> <HotelOccupancy> <RoomCount>1</RoomCount> <Occupancy> <AdultCount>2</AdultCount> <ChildCount>0</ChildCount> </Occupancy> </HotelOccupancy> <HotelRoom SHRUI="x/Sef0dn9jkzCPIf9cUSFg==" onRequest="N"> <Board type="SIMPLE" code="HDE10" shortname="BB">BED AND BREAKFAST</Board> <RoomType type="SIMPLE" code="DBLE10" characteristic="ST">DOUBLE/ TWIN STANDARD</RoomType> <Price> <Amount>324.420</Amount> </Price> </HotelRoom> </AvailableRoom> <AvailableRoom> <HotelOccupancy> <RoomCount>1</RoomCount> <Occupancy> <AdultCount>2</AdultCount> <ChildCount>0</ChildCount> </Occupancy> </HotelOccupancy> <HotelRoom SHRUI="iWQ9UwBipkWWfT9mPwqROA==" onRequest="N"> <Board type="SIMPLE" code="HDE10" shortname="BB">BED AND BREAKFAST</Board> <RoomType type="SIMPLE" code="BUNE10" characteristic="ST">BUNGALOW STANDARD</RoomType> <Price> <Amount>389.320</Amount> </Price> </HotelRoom> </AvailableRoom> <AvailableRoom> <HotelOccupancy> <RoomCount>1</RoomCount> <Occupancy> <AdultCount>2</AdultCount> <ChildCount>0</ChildCount> </Occupancy> </HotelOccupancy> <HotelRoom SHRUI="YBqpzlwubLW+CTG9fdzIow==" onRequest="N"> <Board type="SIMPLE" code="MPE10" shortname="HB">HALF BOARD</Board> <RoomType type="SIMPLE" code="JSUE10" characteristic="ST">JUNIOR SUITE STANDARD</RoomType> <Price> <Amount>502.860</Amount> </Price> </HotelRoom> </AvailableRoom> <AvailableRoom> <HotelOccupancy> <RoomCount>1</RoomCount> <Occupancy> <AdultCount>2</AdultCount> <ChildCount>0</ChildCount> </Occupancy> </HotelOccupancy> <HotelRoom SHRUI="LnBRpdPBffNgsuXZHZPcvw==" onRequest="N"> <Board type="SIMPLE" code="MPE10" shortname="HB">HALF BOARD</Board> <RoomType type="SIMPLE" code="BUNE10" characteristic="ST">BUNGALOW STANDARD</RoomType> <Price> <Amount>535.320</Amount> </Price> </HotelRoom> </AvailableRoom> </ServiceHotel>
With this code:
Dim xd As XElement = XElement.Parse(xml) Dim xm = XNamespace.Get("http://www.hotelbeds.com/schemas/2005/06/messages") Dim result = From item In xd.Descendants(xm + "HotelValuedAvailRS").Elements Select New With { .av = item.@availToken, .ContractName = item.Descendants(xm + "Contract").Elements().Value, .IncomingOfficeID = item.Descendants(xm + "IncomingOffice").@code, .DateFrom = item.Descendants(xm + "DateFrom").@date, .DateTo = item.Descendants(xm + "DateTo").@date, .HotelCode = item.Descendants(xm + "HotelInfo").Elements().Value, .RoomCount = item.Descendants(xm + "AvailableRoom").Elements().Value } For Each item In result Response.Write("Available token: " & item.av & ", Contract Name: " & item.ContractName & ", IncomingOfficeID: " & item.IncomingOfficeID & ", DateFrom: " & item.DateFrom & ", DateTo:" & item.DateTo & ", HotelCode: " & item.HotelCode & ", RoomCount: " & item.RoomCount & "<br/>") Next
The problem is - when, for example, I'm trying to get values of nedsted nodes like that
<AvailableRoom> <HotelOccupancy> <RoomCount>1</RoomCount> <Occupancy> <AdultCount>2</AdultCount> <ChildCount>0</ChildCount> </Occupancy> </HotelOccupancy>
I'm getting values of all three nodes and I don't have idea how to select for example exatly value of <AdultCount> node. All what I did tried bedfore return me in this code
.RoomCount = item.Descendants(xm + "AvailableRoom").Elements().Value
All three nodes.
Any ideas?
All-Star
118619 Points
18779 Posts
Dec 12, 2012 11:21 AM|LINK
Hi,
You have to do to nest another loop to fetch out its inner nodes and have a try like this (by another LINQ-TO-XML statament you cann do that):
.RoomCount = from node in item.Descendants(xm + "AvailableRoom").Element("Occupancy").Elements() select node.Value
Then you have to use foreach to loop and fetch out each value assigned to "RoomCount".
Dec 13, 2012 06:15 AM|LINK
Thanks! Let me try.
Mamboking
Member
183 Points
125 Posts
Problem with iterate over child nodes
Dec 11, 2012 10:05 AM|LINK
Hello!
I'm trying to iterate over value of nested nodes in xml files with such structure:
With this code:
Dim xd As XElement = XElement.Parse(xml) Dim xm = XNamespace.Get("http://www.hotelbeds.com/schemas/2005/06/messages") Dim result = From item In xd.Descendants(xm + "HotelValuedAvailRS").Elements Select New With { .av = item.@availToken, .ContractName = item.Descendants(xm + "Contract").Elements().Value, .IncomingOfficeID = item.Descendants(xm + "IncomingOffice").@code, .DateFrom = item.Descendants(xm + "DateFrom").@date, .DateTo = item.Descendants(xm + "DateTo").@date, .HotelCode = item.Descendants(xm + "HotelInfo").Elements().Value, .RoomCount = item.Descendants(xm + "AvailableRoom").Elements().Value } For Each item In result Response.Write("Available token: " & item.av & ", Contract Name: " & item.ContractName & ", IncomingOfficeID: " & item.IncomingOfficeID & ", DateFrom: " & item.DateFrom & ", DateTo:" & item.DateTo & ", HotelCode: " & item.HotelCode & ", RoomCount: " & item.RoomCount & "<br/>") NextThe problem is - when, for example, I'm trying to get values of nedsted nodes like that
I'm getting values of all three nodes and I don't have idea how to select for example exatly value of <AdultCount> node. All what I did tried bedfore return me in this code
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Problem with iterate over child nodes
Dec 12, 2012 11:21 AM|LINK
Hi,
You have to do to nest another loop to fetch out its inner nodes and have a try like this (by another LINQ-TO-XML statament you cann do that):
.RoomCount = from node in item.Descendants(xm + "AvailableRoom").Element("Occupancy").Elements() select node.ValueThen you have to use foreach to loop and fetch out each value assigned to "RoomCount".
Mamboking
Member
183 Points
125 Posts
Re: Problem with iterate over child nodes
Dec 13, 2012 06:15 AM|LINK
Thanks! Let me try.