使用c#读取XML内部子节点

时间:2015-10-14 06:46:51

标签: c# .net xml

我正在尝试在c#中读取xml。我能够读取节点' ArrayOfCruisePriceSummaryResponse'但我怎样才能阅读内部节点。

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

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

}

使用GetElementsByTagName我能够到达该节点,但我该如何阅读内部孩子。我想从TotalPriceBestFare阅读FullFare每个孩子都有两个内心BestFareFullFare,我需要阅读每个TotalPrice。< / p>

<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我不想使用LINQ因为我正在使用VS2008处理SSIS项目,它不支持LINQ。

1 个答案:

答案 0 :(得分:4)

您可以使用各种SelectNode变体来遍历文档并提取数据。

试试这个:

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

您可能希望查看here以获取有关XPath位置的信息。