如何从此XML中读取值

时间:2017-03-19 20:42:45

标签: c# asp.net xml web-services soap

我正在尝试从soap响应中读取xml。它在下面给出

`<OTA_AirLowFareSearchRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.9.2" PricedItinCount="1" BrandedOneWayItinCount="0" SimpleOneWayItinCount="0" DepartedItinCount="0" SoldOutItinCount="0" AvailableItinCount="0">
    <Success xmlns="http://www.opentravel.org/OTA/2003/05"/>
    <Warnings xmlns="http://www.opentravel.org/OTA/2003/05">...</Warnings>
    <PricedItineraries xmlns="http://www.opentravel.org/OTA/2003/05">
        <PricedItinerary SequenceNumber="1">
                <AirItinerary DirectionInd="OneWay">
                        <OriginDestinationOptions>
                                <OriginDestinationOption ElapsedTime="1920">
                                        <FlightSegment DepartureDateTime="2017-03-21T21:45:00" ArrivalDateTime="2017-03-22T09:50:00" StopQuantity="0" FlightNumber="7336" ResBookDesigCode="T" ElapsedTime="425">
                                                <DepartureAirport LocationCode="CDL" TerminalID="1"/>
                                                <ArrivalAirport LocationCode="CDA" TerminalID="1A"/>
                                                <OperatingAirline Code="AA" FlightNumber="810"/>
                                                <Equipment AirEquipType="000"/>
                                                <MarketingAirline Code="PP"/>
                                                <DisclosureAirline Code="AC"/>
                                                <MarriageGrp>O</MarriageGrp>
                                                <DepartureTimeZone GMTOffset="-10"/>
                                                <ArrivalTimeZone GMTOffset="11"/>
                                                <TPA_Extensions>
                                                        <eTicket Ind="true"/>
                                                </TPA_Extensions>
                                        </FlightSegment>
                                </OriginDestinationOption>
                        </OriginDestinationOptions>
                </AirItinerary>
        </PricedItinerary>
</PricedItineraries>
</OTA_AirLowFareSearchRS>`

我用LINQ to XML尝试过很多东西,但似乎没什么用。

以下是我尝试使用

的示例
var xml = XDocument.Parse(stringXMLResponse);
        var result = from item in xml.Descendants("FlightSegment")
                 select new
                 {
                     v1 = item.FirstAttribute.Value
                 };

我们鼓励任何建议。谢谢。

1 个答案:

答案 0 :(得分:1)

您可以像这样编写LINQ查询。您可能需要调整它的要求,但它适用于您的XML字符串。

var selected = from x in xdoc.Descendants()
              where x.NodeType == XmlNodeType.Element 
              && x.Name.LocalName == "FlightSegment"
              select x;

这是小提琴手:https://dotnetfiddle.net/NxkT38