简体   繁体   中英

Reading XML inner child node using c#

I am trying to read xml in c#. I am able to read the node 'ArrayOfCruisePriceSummaryResponse' but how can i read the inner nodes.

XmlNodeList xmlnode;
xmlnode = xml.GetElementsByTagName("CruisePriceSummaryResponse");

for (int i = 0; i <= xmlnode.Count - 1; i++)
{

}

Using GetElementsByTagName I am able to reach to that node but how can I read the inner child. I want to read TotalPrice from BestFare and FullFare Each child has two innerchilds BestFare and FullFare and I need to read each TotalPrice .

<ArrayOfCruisePriceSummaryResponse
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://schemas.datacontract.org/2004/07/OpenseasAPI.ServiceModel">
    <CruisePriceSummaryResponse>
        <AvailablePromos
            xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <d3p1:string>FLA</d3p1:string>
            <d3p1:string>FLB</d3p1:string>
        </AvailablePromos>
        <Brand>PA</Brand>
        <CruiseCategory i:nil="true"/>
        <RoomSize>
            <CruisePriceSummaryRoomSize>
                <BestFare>
                    <TotalPrice>2798.0000000</TotalPrice>
                </BestFare>
                <FullFare>
                    <TotalPrice>3198.000000</TotalPrice>
                </FullFare>
                <PaxCount>2</PaxCount>
            </CruisePriceSummaryRoomSize>
            <CruisePriceSummaryRoomSize>
                <BestFare>
                    <TotalPrice>2796.000000</TotalPrice>
                </BestFare>
                <FullFare>
                    <TotalPrice>4196.000000</TotalPrice>
                </FullFare>
                <PaxCount>4</PaxCount>
            </CruisePriceSummaryRoomSize>
        </RoomSize>
        <ShipCode>PD</ShipCode>
    </CruisePriceSummaryResponse>
    <CruisePriceSummaryResponse>
        <AvailablePromos
            xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <d3p1:string>FLA</d3p1:string>
            <d3p1:string>LF1</d3p1:string>
        </AvailablePromos>
        <Brand>PA</Brand>
        <RoomSize>
            <CruisePriceSummaryRoomSize>
                <BestFare>
                    <TotalPrice>1298.000000</TotalPrice>
                </BestFare>
                <FullFare>
                    <TotalPrice>3498.000000</TotalPrice>
                </FullFare>
                <PaxCount>2</PaxCount>
            </CruisePriceSummaryRoomSize>
            <CruisePriceSummaryRoomSize>
                <BestFare>
                    <TotalPrice>1796.000000</TotalPrice>
                </BestFare>
                <FullFare>
                    <TotalPrice>5396.000000</TotalPrice>
                </FullFare>
                <PaxCount>4</PaxCount>
            </CruisePriceSummaryRoomSize>
        </RoomSize>
        <ShipCode>PJ</ShipCode>
    </CruisePriceSummaryResponse>
</ArrayOfCruisePriceSummaryResponse>

PS I dont not want to use LINQ because I am working on a SSIS project using VS2008 and it does not support LINQ.

You can use the various SelectNode variants to traverse your document and extract your data.

Try this:

XmlNodeList xmlnodes;
xmlnodes = xml.GetElementsByTagName("CruisePriceSummaryResponse");

for (int i = 0; i < xmlnodes.Count; i++)
{
    XmlNodeList rooms = xmlnodes[i].SelectNodes("RoomSize/CruisePriceSummaryRoomSize");
    for(int j = 0; j < rooms.Count; j++)
    {
        string bestFare = rooms[j].SelectSingleNode("BestFare/TotalPrice").InnerText;
        string fullFare = rooms[j].SelectSingleNode("FullFare/TotalPrice").InnerText;

        // do whatever you need
    }
}

You might want to look here for information about XPath locations.

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