这可以缩短(使用VB.NET LINQ to XML)?

时间:2014-02-21 21:33:50

标签: vb.net linq-to-xml

我有以下代码,顺便说一下,但我正在努力缩短它并使用XML简写。到目前为止一切都很好,但有没有办法缩短最后几个项目,看起来像是一大堆Element调用:

Public Function ParseRates() As IEnumerable(Of RoomRate)
    Try
        For Each n As XElement In _xDoc...<_ns:RoomRate>
            _rates.Add(New RoomRate With { _
                        .GuaranteeSurchargeRequired = n.@GuaranteeSurchargeRequired, _
                        .IATACharacteristicIdentification = n.@IATACharacteristicIdentification, _
                        .IATAProductIdentification = n.@IATAProductIdentification, _
                        .RPH = n.@RPH, _
                        .CancellationPolicy = n...<_ns:AdditionalInfo>.<_ns:CancelPolicy>.@Numeric, _
                        .Commission = n...<_ns:AdditionalInfo>.<_ns:Commission>.@NonCommission, _
                        .Rate = n.Element(_ns + "Rates").Element(_ns + "Rate").Attribute("Amount").Value, _
                        .CurrencyCode = n.Element(_ns + "Rates").Element(_ns + "Rate").Attribute("CurrencyCode").Value,
                        .TotalPrice = n.Element(_ns + "Rates").Element(_ns + "Rate").Element(_ns + "HotelTotalPricing").Attribute("Amount").Value, _
                        .Disclaimer = n.Element(_ns + "Rates").Element(_ns + "Rate").Element(_ns + "HotelTotalPricing").Element(_ns + "Disclaimer").Value, _
                        .Surcharge = n.Element(_ns + "Rates").Element(_ns + "Rate").Element(_ns + "HotelTotalPricing").Element(_ns + "TotalSurcharges").Attribute("Amount").Value})
        Next
        Return _rates
    Catch ex As Exception
        ErrorMessage = ex.Message
        Return Nothing
    End Try
End Function

以下是使用所述代码解析的XML片段

<HotelPropertyDescriptionRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" TimeStamp="2013-12-30T18:49:36" Version="1.14.1">
  <Success xmlns="http://webservices.sabre.com/sabreXML/2003/07" />
  <RoomStay xmlns="http://webservices.sabre.com/sabreXML/2003/07">
<RoomRates>
  <RoomRate GuaranteeSurchargeRequired="G" IATACharacteristicIdentification="BGGO00" IATAProductIdentification="BLOOMBERG" RPH="001">
    <AdditionalInfo>
      <CancelPolicy Numeric="06" />
      <Commission NonCommission="true">NON COMMISSIONABLE</Commission>
      <Text>BLOOMBERG LP, 0.0 KM, INCLUDES BREAKFAST, INTERNET, WIFI, SEE</Text>
      <Text>RATE RULES DELUXE ROOM, GUEST ROOM, 1 KING OR 2 TWIN/SINGLE BE</Text>
    </AdditionalInfo>
    <Rates>
      <Rate Amount="66.600" CurrencyCode="KWD">
        <AdditionalGuestAmounts>
          <AdditionalGuestAmount MaxExtraPersonsAllowed="0">
            <Charges ExtraPerson="0" />
          </AdditionalGuestAmount>
        </AdditionalGuestAmounts>
        <HotelTotalPricing Amount="76.590">
          <Disclaimer>INCLUDES TAXES AND SURCHARGES</Disclaimer>
          <TotalSurcharges Amount="9.990" />
        </HotelTotalPricing>
      </Rate>
    </Rates>
  </RoomRate>
</RoomRates>
</HotelPropertyDescriptionRS>

2 个答案:

答案 0 :(得分:1)

谈到这一部分:

.Rate = n.Element(_ns + "Rates").Element(_ns + "Rate").Attribute("Amount").Value, _
.CurrencyCode = n.Element(_ns + "Rates").Element(_ns + "Rate").Attribute("CurrencyCode").Value,
.TotalPrice = n.Element(_ns + "Rates").Element(_ns + "Rate").Element(_ns + "HotelTotalPricing").Attribute("Amount").Value, _
.Disclaimer = n.Element(_ns + "Rates").Element(_ns + "Rate").Element(_ns + "HotelTotalPricing").Element(_ns + "Disclaimer").Value, _
.Surcharge = n.Element(_ns + "Rates").Element(_ns + "Rate").Element(_ns + "HotelTotalPricing").Element(_ns + "TotalSurcharges").Attribute("Amount").Value})

还有其他一些方法可以获得这5个值。首先,使用.Descendants()并获取返回的第一个元素:

.Rate = n.Descendants(_ns + "Rate").First().Attribute("Amount").Value, _
.CurrencyCode = n.Descendants(_ns + "Rate").First().Attribute("CurrencyCode").Value,
.TotalPrice = n.Descendants(_ns + "HotelTotalPricing").First().Attribute("Amount").Value, _
.Disclaimer = n.Descendants(_ns + "Disclaimer").First().Value, _
.Surcharge = n.Descendants(_ns + "TotalSurcharges").First().Attribute("Amount").Value})
.Descendants()仅搜索当前元素的直接子元素时,

Element()可以为您带来当前元素的任何子元素。其他选项是使用XPath:

.Rate = n.XPathSelectElement(".//n:Rate", nsm).Attribute("Amount").Value, _
.CurrencyCode = n.XPathSelectElement(".//n:Rate", nsm).Attribute("CurrencyCode").Value,
.TotalPrice = n.XPathSelectElement(".//n:HotelTotalPricing", nsm).Attribute("Amount").Value, _
.Disclaimer = n.XPathSelectElement(".//n:Disclaimer", nsm).Value, _
.Surcharge = n.XPathSelectElement(".//n:TotalSurcharges", nsm).Attribute("Amount").Value

在这种情况下,您需要首先定义要在XPath表达式中使用的前缀:

Dim nsm As XmlNamespaceManager = New XmlNamespaceManager(New NameTable)
nsm.AddNamespace("n", "http://webservices.sabre.com/sabreXML/2003/07")

并添加Imports

Imports System.Xml.XPath

从方法样式切换到XPath很简单。这是关于XPath语法的基础知识:

    表达式中的
  1. dot(.)告诉从当前节点搜索。如果没有它,将从根节点
  2. 评估表达式
  3. 双斜杠(//)类似于Descendants()
  4. 单斜杠(/)类似于Element()Elements()

答案 1 :(得分:1)

如何在其中使用AddRange和LINQ查询?

您可以使用Let来简化您的查询:

Dim query = From n In _xDoc...<_ns:RoomRate>
            Let additionalInfo = n...<_ns:AdditionalInfo>
            Let rate = n.<_ns:Rates>.<_ns:Rate>
            Let hotelPricing = rate.<_ns:HotelTotalPricing>
            Select New RoomRate With {
                .GuaranteeSurchargeRequired = n.@GuaranteeSurchargeRequired, _
                .IATACharacteristicIdentification = n.@IATACharacteristicIdentification, _
                .IATAProductIdentification = n.@IATAProductIdentification, _
                .RPH = n.@RPH, _
                .CancellationPolicy = additionalInfo.<_ns:CancelPolicy>.@Numeric, _
                .Commission = additionalInfo.<_ns:Commission>.@NonCommission, _
                .Rate = rate.@Amount, _
                .CurrencyCode = rate.@CurrencyCode,
                .TotalPrice = hotelPricing.@Amount, _
                .Disclaimer = hotelPricing.<_ns:Disclaimer>.Value, _
                .Surcharge = hotelPricing.<_ns:TotalSurcharges>.@Amount
            }

它需要名称空间导入声明:

Imports <xmlns:_ns="test">

您应该可以在AddRange上致电_rates

_rates.AddRange(query)