Reading XML inner child node using c#
我正在尝试在 c# 中读取 xml。我能够读取节点 \\'ArrayOfCruisePriceSummaryResponse\\' 但我如何读取内部节点。
1 2 3 4 5 6 7 | XmlNodeList xmlnode; xmlnode = xml.GetElementsByTagName("CruisePriceSummaryResponse"); for (int i = 0; i <= xmlnode.Count - 1; i++) { } |
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <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 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 位置的信息。