【问题标题】:How to read values from this XML如何从此 XML 中读取值
【发布时间】:2017-08-11 01:09:11
【问题描述】:

我正在尝试从肥皂响应中读取 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
                 };

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

【问题讨论】:

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


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多