0

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

4

1 回答 1

0

您永远不会加载或读取源 XML。你的代码

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

创建一个空的 XML 文档,然后尝试从空的 xml 中获取元素。

您需要调用XmlDocument.LoadXmlDocument.LoadXML从文件或字符串中读取 xml。

XmlDocument xml = new XmlDocument();
xml.Load("pathtosomefile.xml");
XmlNodeList xmlnode = xml.GetElementsByTagName("CruisePriceSummaryResponse");
于 2015-10-14T22:58:33.717 回答