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

本文关键字:内部 子节点 XML 读取 使用 | 更新日期: 2023-09-27 18:17:40

我正在尝试在c#中读取xml。我能够读取节点' arrayofcruispricesummaryresponse ',但我怎么能读取内部节点。

XmlNodeList xmlnode;
xmlnode = xml.GetElementsByTagName("CruisePriceSummaryResponse");
for (int i = 0; i <= xmlnode.Count - 1; i++)
{
}

使用GetElementsByTagName,我能够到达该节点,但我如何读取内部子节点。我想从BestFareFullFare中读取TotalPrice每个孩子有两个innerchild BestFareFullFare,我需要读取每个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。我不想使用LINQ,因为我正在使用VS2008的SSIS项目,它不支持LINQ。

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

您可以使用各种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
    }
}

您可能希望在这里查看有关XPath位置的信息。