0

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 FullFareand 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>

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

1
  • Would you mind using the XmlSerializer? Commented Oct 14, 2015 at 6:57

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Amit. i am getting error 'System.Xml.XmlNodeList' does not contain a definition for 'SelectNodes' i declared XmlDocument xml = new XmlDocument(); and then XmlNodeList rooms = xml.SelectNodes("RoomSize/CruisePriceSummaryRoomSize");. but this is returning 0 value
@OwaisAhmed - You're right, I forgot the accessor ([i]). Your naming scheme confused me, so I renamed the variable and added the missing part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.