XML父节点和子节点读取

时间:2014-11-10 11:15:16

标签: c# xml parent

XML数据

<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#代码

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;
    }

}

我想阅读每个酒店信息中的图像列表下的每个细节。但我的第二个foreach循环所有图像列表细节,而没有移动到第一个foreach循环的下一个酒店信息。

1 个答案:

答案 0 :(得分:1)

当你这样做时:

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

您请求.../ns:Image的所有xdcDocument代码,如果您只想选择每个ns:Image的{​​{1}},则必须使用xndNode SelectNodes方法xndNode 1}}:

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

请注意xPath中的更改,因为现在我们已经处于ns:HotelInfo级别。