问题描述
<OTA_AirAvailRS><OriginDestinationInformation><DepartureDateTime>2015-05-05</DepartureDateTime><OriginLocationLocationCode="BJS"/><DestinationLocationLocationCode="SHA"/><OriginDestinationOptions><OriginDestinationOption><FlightSegmentDepartureDateTime="2015-05-05T11:00:00"ArrivalDateTime="2015-05-05T13:10:00"StopQuantity="0"ASRInd="true"ParticipationLevelCode="DS#"CodeshareInd="false"FlightNumber="5108"Ticket="eTicket"MealCode="L"FlightTime="PT2H10M"><DepartureAirportLocationCode="PEK"Terminal="T2"/><ArrivalAirportLocationCode="SHA"Terminal="T2"/><EquipmentAirEquipType="333"/><MarketingAirlineCode="MU"/><BookingClassAvailResBookDesigCode="U"ResBookDesigQuantity="C"/><BookingClassAvailResBookDesigCode="F"ResBookDesigQuantity="A"/><BookingClassAvailResBookDesigCode="P"ResBookDesigQuantity="S"/><BookingClassAvailResBookDesigCode="J"ResBookDesigQuantity="3"/><BookingClassAvailResBookDesigCode="C"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="D"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="I"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="W"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="Y"ResBookDesigQuantity="A"/><BookingClassAvailResBookDesigCode="B"ResBookDesigQuantity="A"/><BookingClassAvailResBookDesigCode="M"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="E"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="H"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="K"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="L"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="N"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="R"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="S"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="V"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="T"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="G"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="Z"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="Q"ResBookDesigQuantity="1"/></FlightSegment></OriginDestinationOption><OriginDestinationOption><FlightSegmentDepartureDateTime="2015-05-05T11:00:00"ArrivalDateTime="2015-05-05T13:10:00"StopQuantity="0"ASRInd="true"ParticipationLevelCode="DS#"CodeshareInd="true"FlightNumber="9277"Ticket="eTicket"MealCode="L"FlightTime="PT2H10M"><OperatingAirlineCode="MU"FlightNumber="5108"/><DepartureAirportLocationCode="PEK"Terminal="T2"/><ArrivalAirportLocationCode="SHA"Terminal="T2"/><EquipmentAirEquipType="333"/><MarketingAirlineCode="CZ"/><BookingClassAvailResBookDesigCode="Y"ResBookDesigQuantity="A"/><BookingClassAvailResBookDesigCode="B"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="M"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="U"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="L"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="E"ResBookDesigQuantity="Q"/></FlightSegment></OriginDestinationOption><OriginDestinationOption><FlightSegmentDepartureDateTime="2015-05-05T11:30:00"ArrivalDateTime="2015-05-05T13:40:00"StopQuantity="0"ASRInd="true"ParticipationLevelCode="DS#"CodeshareInd="true"FlightNumber="1903"Ticket="eTicket"MealCode="L"FlightTime="PT2H10M"><OperatingAirlineCode="CA"FlightNumber="1557"/><DepartureAirportLocationCode="PEK"Terminal="T3"/><ArrivalAirportLocationCode="SHA"Terminal="T2"/><EquipmentAirEquipType="747"/><MarketingAirlineCode="HO"/><BookingClassAvailResBookDesigCode="Y"ResBookDesigQuantity="A"/><BookingClassAvailResBookDesigCode="B"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="M"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="T"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="E"ResBookDesigQuantity="Q"/><BookingClassAvailResBookDesigCode="V"ResBookDesigQuantity="Q"/></FlightSegment></OriginDestinationOption></OriginDestinationOptions></OriginDestinationInformation></OTA_AirAvailRS>比如上面这一段XML文本,我想取出DepartureDateTime="2015-05-05T11:00:00"ArrivalDateTime="2015-05-05T13:10:00"FlightNumber="5108"<OperatingAirlineCode="MU"FlightNumber="5108"/><MarketingAirlineCode="CZ"/>以及(ResBookDesigCode="U"ResBookDesigQuantity="C"ResBookDesigCode="F"ResBookDesigQuantity="A")所有值,这种格式也可以uc,FA,PS,J3等所有舱位和舱等的值,怎么实现啊???求大神帮忙啊,正在表达式怎么写啊高分求助
解决方案
解决方案二:
这是xml就用xml的写法呗,干嘛要用正则
解决方案三:
staticvoidReadOTAData(){stringxml=@"<OTA_AirAvailRS><OriginDestinationInformation><DepartureDateTime>2015-05-05</DepartureDateTime><OriginLocationLocationCode=""BJS""/><DestinationLocationLocationCode=""SHA""/><OriginDestinationOptions><OriginDestinationOption><FlightSegmentDepartureDateTime=""2015-05-05T11:00:00""ArrivalDateTime=""2015-05-05T13:10:00""StopQuantity=""0""ASRInd=""true""ParticipationLevelCode=""DS#""CodeshareInd=""false""FlightNumber=""5108""Ticket=""eTicket""MealCode=""L""FlightTime=""PT2H10M""><DepartureAirportLocationCode=""PEK""Terminal=""T2""/><ArrivalAirportLocationCode=""SHA""Terminal=""T2""/><EquipmentAirEquipType=""333""/><MarketingAirlineCode=""MU""/><BookingClassAvailResBookDesigCode=""U""ResBookDesigQuantity=""C""/><BookingClassAvailResBookDesigCode=""F""ResBookDesigQuantity=""A""/><BookingClassAvailResBookDesigCode=""P""ResBookDesigQuantity=""S""/><BookingClassAvailResBookDesigCode=""J""ResBookDesigQuantity=""3""/><BookingClassAvailResBookDesigCode=""C""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""D""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""I""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""W""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""Y""ResBookDesigQuantity=""A""/><BookingClassAvailResBookDesigCode=""B""ResBookDesigQuantity=""A""/><BookingClassAvailResBookDesigCode=""M""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""E""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""H""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""K""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""L""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""N""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""R""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""S""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""V""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""T""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""G""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""Z""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""Q""ResBookDesigQuantity=""1""/></FlightSegment></OriginDestinationOption><OriginDestinationOption><FlightSegmentDepartureDateTime=""2015-05-05T11:00:00""ArrivalDateTime=""2015-05-05T13:10:00""StopQuantity=""0""ASRInd=""true""ParticipationLevelCode=""DS#""CodeshareInd=""true""FlightNumber=""9277""Ticket=""eTicket""MealCode=""L""FlightTime=""PT2H10M""><OperatingAirlineCode=""MU""FlightNumber=""5108""/><DepartureAirportLocationCode=""PEK""Terminal=""T2""/><ArrivalAirportLocationCode=""SHA""Terminal=""T2""/><EquipmentAirEquipType=""333""/><MarketingAirlineCode=""CZ""/><BookingClassAvailResBookDesigCode=""Y""ResBookDesigQuantity=""A""/><BookingClassAvailResBookDesigCode=""B""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""M""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""U""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""L""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""E""ResBookDesigQuantity=""Q""/></FlightSegment></OriginDestinationOption><OriginDestinationOption><FlightSegmentDepartureDateTime=""2015-05-05T11:30:00""ArrivalDateTime=""2015-05-05T13:40:00""StopQuantity=""0""ASRInd=""true""ParticipationLevelCode=""DS#""CodeshareInd=""true""FlightNumber=""1903""Ticket=""eTicket""MealCode=""L""FlightTime=""PT2H10M""><OperatingAirlineCode=""CA""FlightNumber=""1557""/><DepartureAirportLocationCode=""PEK""Terminal=""T3""/><ArrivalAirportLocationCode=""SHA""Terminal=""T2""/><EquipmentAirEquipType=""747""/><MarketingAirlineCode=""HO""/><BookingClassAvailResBookDesigCode=""Y""ResBookDesigQuantity=""A""/><BookingClassAvailResBookDesigCode=""B""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""M""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""T""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""E""ResBookDesigQuantity=""Q""/><BookingClassAvailResBookDesigCode=""V""ResBookDesigQuantity=""Q""/></FlightSegment></OriginDestinationOption></OriginDestinationOptions></OriginDestinationInformation></OTA_AirAvailRS>";XElementroot=XElement.Parse(xml);foreach(varfirstElementinroot.Elements("OriginDestinationInformation")){//Console.WriteLine("DepartureDateTime:"+firstElement.Element("DepartureDateTime").Value);varoptions=firstElement.Element("OriginDestinationOptions");if(options!=null){foreach(varoptioninoptions.Elements("OriginDestinationOption")){varsegment=option.Element("FlightSegment");Console.WriteLine("DepartureDateTime:"+segment.Attribute("DepartureDateTime").Value);Console.WriteLine("ArrivalDateTime:"+segment.Attribute("ArrivalDateTime").Value);varoperatingAirline=segment.Element("OperatingAirline");Console.WriteLine("Code:"+segment.Attribute("FlightNumber").Value);Console.WriteLine("FlightNumber:"+segment.Attribute("FlightNumber").Value);foreach(varbookinsegment.Elements("BookingClassAvail")){Console.WriteLine("ResBookDesigCode:"+book.Attribute("ResBookDesigCode").Value);Console.WriteLine("ResBookDesigQuantity:"+book.Attribute("ResBookDesigQuantity").Value);}}}}}