Saturday, 15 May 2010

c# - XML Parent node and sub node reading -



c# - XML Parent node and sub node reading -

xml data

<hotelvaluedavailrs xmlns="http://www.hotelbeds.com/schemas/2005/06/messages" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.hotelbeds.com/schemas/2005/06/messages hotelvaluedavailrs.xsd" timetoexpiration="1799971" totalitems="90" echotoken="dummyechotoken"> <auditdata> <processtime>275</processtime> <timestamp>2014-11-04 11:39:28.253</timestamp> <schemarelease>2005/06</schemarelease> <hydracorerelease>2014.07.10.pci</hydracorerelease> <hydraenumerationsrelease>n/a</hydraenumerationsrelease> <merlinrelease>n/a</merlinrelease> </auditdata> <paginationdata currentpage="1" totalpages="5" /> <servicehotel xsi:type="servicehotel" availtoken="18s4jo2avqezlwqdtnj3bwbh"> <contractlist> <contract> <name>id_b2b_24#bari</name> <incomingoffice code="1"></incomingoffice> <classification code="nor">online price</classification> </contract> </contractlist> <datefrom date="20141228" /> <dateto date="20141230" /> <currency code="eur">euro</currency> <hotelinfo xsi:type="producthotel"> <code>271</code> <name>tryp palma bellver hotel</name> <imagelist> <image> <type>jpg</type> <order>1</order> <visualizationorder>1</visualizationorder> <url>jpg</url> </image> <image> <type>jpg</type> <order>2</order> <visualizationorder>2</visualizationorder> <url>jpg</url> </image> </imagelist> </hotelinfo> <hotelinfo xsi:type="producthotel"> <code>272</code> <name>beach hotel</name> <imagelist> <image> <type>jpg</type> <order>3</order> <visualizationorder>3</visualizationorder> <url>jpg</url> </image> <image> <type>jpg</type> <order>4</order> <visualizationorder>4</visualizationorder> <url>jpg</url> </image> </imagelist> </hotelinfo> </servicehotel> </hotelvaluedavailrs>

c# code

xmldocument xdcdocument = new xmldocument(); xdcdocument.load(@"e:\\hotel.xml"); var nsmgr = new xmlnamespacemanager(xdcdocument.nametable); nsmgr.addnamespace("ns", "http://www.hotelbeds.com/schemas/2005/06/messages"); var nl = xdcdocument.selectnodes("/ns:hotelvaluedavailrs/ns:servicehotel/ns:hotelinfo", nsmgr); foreach (xmlnode xndnode in nl) { string name = xndnode["name"].innertext; var nl3 = xdcdocument.selectnodes("/ns:hotelvaluedavailrs/ns:servicehotel/ns:hotelinfo/ns:imagelist/ns:image", nsmgr); foreach (xmlnode xndnode3 in nl3) { string url = xndnode3["url"].innertext; string order = xndnode3["order"].innertext; } }

i want read every details comes under images list in every hotel info. sec foreach looping images list details without moving next hotel info first foreach loop.

when do:

var nl3 = xdcdocument.selectnodes("/ns:hotelvaluedavailrs/ns:servicehotel/ns:hotelinfo/ns:imagelist/ns:image", nsmgr);

you request .../ns:image tags of xdcdocument, if want select ns:image of each xndnode must utilize selectnodes method of xndnode:

var nl3 = xndnode.selectnodes("ns:imagelist/ns:image", nsmgr);

note alter in xpath because in ns:hotelinfo level.

c# xml parent

No comments:

Post a Comment