0

我在 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 节点的整个内部 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>
                          :
                          :
                          :
                          :
4

1 回答 1

0

"ns1"XmlNamespaceManager. 根据您尝试解析的 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。

于 2013-03-05T12:39:54.977 回答