想要提取xml文件的整个节点

时间:2013-03-05 12:35:53

标签: xml vb.net

我在vb.net中有这样的代码

Dim str As String

        Dim xmlText As String = File.ReadAllText(Server.MapPath("~/reply.xml"))
        Dim doc As New XmlDocument()

        doc.LoadXml(xmlText)



        Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
        nsmgr.AddNamespace("ns1", "http://schemas.xmlsoap.org/soap/envelope/")
        nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
        Dim root As XmlElement = doc.DocumentElement

        Dim node As XmlNode = root.SelectSingleNode("//soapenv:Body", nsmgr)
        Dim s As String = node.InnerXml

它适用于我我能够提取soapenv:Body node的整个内部xml。但我想提取soapenv的子子节点:Body节点。

我的表达方式如下:

 Dim node As XmlNode = root.SelectSingleNode("//soapenv:Body/ns1:OTA_AirLowFareSearchRS/ns1:PricedItineraries", nsmgr)

但它什么都没提取。我的xml是这样的:

<?xml version="1.0" encoding="utf-8" ?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
    <ns1:OTA_AirLowFareSearchRS Version="2.001" xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:ns1="http://www.opentravel.org/OTA/2003/05">
      <ns1:Success />
        <ns1:PricedItineraries>
        <ns1:PricedItinerary CompositeFlightNumber="S2" CountCompositeFlightNumber="1" FareType="Non-Refundable" InboundSegmentReference="1" MatrixFare="true" Mode="" OriginDestinationRPH="BLRMAAS242336620130329" OutboundSegmentReference="1" Priority="1" RecommendationRPH="1" Refundable="true" ReturnOnly="false" SequenceNumber="1.0" SupplierCode="1AWS">
          <ns1:AirItinerary SupplierSystem="Amadeus">
            <ns1:OriginDestinationOptions>
            <ns1:OriginDestinationOption Duration="00:45:00" FlightID="BLRMAAS24233662013-03-29" MajorityCarrier="S2" ReturnOnly="false" SupplierCode="1AWS" SupplierSystem="Amadeus" UniqueIdentifier="1.0">
                <ns1:FlightSegment ArrivalDateTime="2013-03-29T20:20:00" CabinCode="Y" DeliveryMethod="Courier" DepartureDateTime="2013-03-29T19:35:00" Duration="00:45:00" FlightNumber="4233" LTD="1AWS" LineNumber="Y" NumberInParty="BLRMAA" RPH="1" ResBookDesigCode="G" TicketType="Physical" ValidConnectionInd="1AWS">
                <ns1:DepartureAirport AirPortName="Bengaluru" CityName="Bangalore" LocationCode="BLR" />
                <ns1:ArrivalAirport AirPortName="Chennai" CityName="Chennai" LocationCode="MAA" Terminal="D" />
                <ns1:OperatingAirline Code="S2" />
                <ns1:BookingClassAvail FareType="RP" ResBookDesigCode="G" ResBookDesigQuantity="7" Status="7" WebFareName="G2SAP30" />
                <ns1:Equipment AirEquipType="739" />
                <ns1:MarketingAirline Code="S2" MatrixCode="66" Name="JetKonnect" YTAirlineCode="77" />
                <ns1:ValidatingCarrier Code="S2" />
              </ns1:FlightSegment>
                <ns1:FormData>
                <ns1:FBC Destination="MAA" FlightNumber="4233" LineNumber="Y" Origin="BLR" SeatToSell="7" WebFareName="G2SAP30" />
                 <ns1:FareDifference>
                  <ns1:TotalFare ADT="2" BaseFare="1140-ADT 570" CHD="0" Cabin="Economy" HostName="railserver" INF="0" Rbd="RP - G -" Tax="TTL-4806">5957</ns1:TotalFare>
                </ns1:FareDifference>
                <ns1:TicketingInfo DeliveryMethod="Courier" TicketTimeLimit="2013-03-05 4:19:00" TicketType="Physical" />
                <ns1:AgentMarkup>
                   <ns1:Airlines>
                          :
                          :
                          :
                          :

1 个答案:

答案 0 :(得分:0)

您在"ns1"上为XmlNamespaceManager设置了错误的命名空间URI。根据您要解析的XML文档,它应该是:

nsmgr.AddNamespace("ns1", "http://www.opentravel.org/OTA/2003/05")

前缀ns1:用于XML和XPath,但这从根本上并不重要。重要的是,您在XPath中使用的前缀绑定到正确的名称空间URI。

您可以像这样编写代码:

nsmgr.AddNamespace("zzz", "http://www.opentravel.org/OTA/2003/05")
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")

然后像这样的XPath:

//soap:Body/zzz:OTA_AirLowFareSearchRS/zzz:PricedItineraries"

只要URI与文档中的元素匹配,它仍然有效。

请记住:文档中的前缀并不重要;只有名称空间URI很重要。您可以决定将哪些前缀用于XPath,只要它们绑定到您期望的元素的正确URI即可。