Search code examples
c#.netxmlxml-parsingselectnodes

SelectNodes return null


I am using SelectNodes to read xml nodes but i am getting null when i try GetElementsByTagName i get the values.

XmlDocument xml = new XmlDocument();
xml.Load(DownloadFile);
XmlNodeList xmlnode;
xmlnode = xml.GetElementsByTagName("CruisePriceSummaryResponse");

for (int i = 0; i < xmlnode.Count; i++)
{
    XmlNodeList rooms = xml .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
    }
}

I want to read TotalPrice from BestFare and FullFare Each child has two innerchilds BestFare and FullFareand I need to read each TotalPrice.

This is my XML

<?xml version="1.0" encoding="utf-8"?>
<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>

Help would be appreciated. I do not want to use linq as this is a SSIS project using VS2008 and it doesnot support linq.

Thanks in advance


Solution

  • You never load or read the source XML. Your code

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

    creates an empty XML document, and then tries to get elements from the empty xml.

    You need to call XmlDocument.Load or XmlDocument.LoadXML to read the xml from a file or a string.

    XmlDocument xml = new XmlDocument();
    xml.Load("pathtosomefile.xml");
    XmlNodeList xmlnode = xml.GetElementsByTagName("CruisePriceSummaryResponse");