关于C#XML文件取值

问题描述

<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);}}}}}

时间: 2024-11-10 05:31:19

关于C#XML文件取值的相关文章

javascript xml xsl取值及数据修改第1/2页_XML示例

1.example.xml--主要数据文件 <?xml version="1.0" encoding="UTF-8"?> <?xml:stylesheet type="text/xsl" href="example.xsl"?> <projects id="1">     <project title="一级标题1" index="1&

asp取动态表单中数据并写入xml文件,用xsl显示_应用技巧

<html> <head> <title>无标题文档</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <script language="javascript"> var curRow=null; var y=1; function selectRow(){ var e=

asp.net 将一个图片以二进制值的形式存入Xml文件中的实例代码

这篇文章介绍了将一个图片以二进制值的形式存入Xml文件中的实例代码,有需要的朋友可以参考一下   复制代码 代码如下: try    {     int readByte = 0;        //     int bytesToRead = 100;       //数据缓冲区大小     string fileName = "../../WriteXml.xml";   //要打开的文件     //   this.textBox1.Text = string.Empty;   

aspnet-asp.net两个项目后台在cs文件里怎么用cookies传值 取值是固定的

问题描述 asp.net两个项目后台在cs文件里怎么用cookies传值 取值是固定的 取值的项目 被编译过了 代码改不了 下面是取值代码 存值 long p_UserId = Convert.ToInt64(Cookies.Read("Clerk", "CurOrganID")); int p_Degree = Convert.ToInt32(Cookies.Read("Clerk", "Degree")); base.Cl

XML文件修改节点属性值(多种方法)_实用技巧

xml 文件内容: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <subtitles> <info> <content>最新通告:五一放假七天!请各教员悉知</content> <speed>4</speed> <color>red</color> </info> </subtitles

使用qt5操作xml文件,删除xml文件中的节点和修改xml文件节点的数据值

问题描述 使用qt5操作xml文件,删除xml文件中的节点和修改xml文件节点的数据值 使用qt5操作xml文件,删除xml文件中的节点和修改xml文件中节点的数据值. 那位大神知道,如果有代码就更好了. 谢谢 解决方案 QT XML文件 修改节点修改XML文件的节点属性值

android工程文件的res文件下的xml文件中color标签的值如何动态修改

问题描述 android工程文件的res文件下的xml文件中color标签的值如何动态修改 请问给位前辈,如果想动态修改android的strings.xml文件中的color属性java代码该怎莫写呀!?请大家指点一下!是修改传入新值,不是获取. 解决方案 在values下面新建一个叫做color.xml的文件内容如下颜色都是16进制的,你可以通过颜色表来查询. <?xml version="1.0" encoding="utf-8" ?> #FFFF

为什么在xml文件的根节点加了一个属性值就读取不到其子节点的数据了呢

问题描述 为什么在xml文件的根节点加了一个属性值(showStyle="T")就读取不到其子节点的数据了呢<?xmlversion="1.0"encoding="utf-8"?><huashuaishowStyle="T"><CustomerServiceCId="1"><Id>1</Id><UserName>张三</UserN

求教如何将复杂的数据做成xml文件作为接口的返回值

问题描述 再做网络接口,接口数据返回值的格式要求是这样的:{{"Name":"用户名","type":"类型代码","Eva":"等级代码","EvaCount":"评价次数"},{"Name":"用户名","type":"类型代码","Eva":&