简体   繁体   中英

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 to read every details comes under Images list in every hotel info. But my second foreach looping all the images list details without moving to the next hotel info with first foreach loop.

When you do:

var nl3 = xdcDocument.SelectNodes("/ns:HotelValuedAvailRS/ns:ServiceHotel/ns:HotelInfo/ns:ImageList/ns:Image", nsmgr);

You request all .../ns:Image tags of xdcDocument , if you want to select only ns:Image of each xndNode you must use SelectNodes method of the xndNode :

var nl3 = xndNode.SelectNodes("ns:ImageList/ns:Image", nsmgr);

Note the change in the xPath because now we are already in the ns:HotelInfo level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM