Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这可以缩短吗(使用VB.NET将LINQ转换为XML)?_Vb.net_Linq To Xml - Fatal编程技术网

这可以缩短吗(使用VB.NET将LINQ转换为XML)?

这可以缩短吗(使用VB.NET将LINQ转换为XML)?,vb.net,linq-to-xml,Vb.net,Linq To Xml,我有下面的代码,顺便说一下,它可以工作,但我正在努力使它更短,并使用XML速记。到目前为止一切都很好,但是否有办法缩短最后几项,似乎有很多元素调用: Public Function ParseRates() As IEnumerable(Of RoomRate) Try For Each n As XElement In _xDoc...<_ns:RoomRate> _rates.Add(New RoomRate With { _

我有下面的代码,顺便说一下,它可以工作,但我正在努力使它更短,并使用XML速记。到目前为止一切都很好,但是否有办法缩短最后几项,似乎有很多元素调用:

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
公共函数ParseRates()作为IEnumerable(指RoomRate)
尝试
对于_xDoc中的每个n作为元素。。。
_使用{_
.GuaranteSourceRequired=n.@GuaranteSourceRequired_
.IATACharacteristicIdentification=n.@IATACharacteristicIdentification_
.IATAProductIdentification=n.@IATAProductIdentification_
.RPH=n.@RPH_
.CancellationPolicy=n..@数值_
.Commission=n.…@非委任_
.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_
.附加费=n.Element(_ns+“费率”).Element(_ns+“费率”).Element(_ns+“HotelTotalPricing”).Element(_ns+“总附加费”).Attribute(“金额”).Value})
下一个
回报率
特例
ErrorMessage=ex.Message
一无所获
结束尝试
端函数
下面是用上述代码解析的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>

不可佣金
BLOOMBERG LP,0.0公里,包括早餐、互联网、WIFI,请参见
价格规则豪华房、客房、1张大床或2张双人床/单人床
包括税费和附加费

谈到这一部分:

.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个值。首先,使用
.subjections()
并获取返回的第一个元素:

.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})
元素()
仅搜索当前元素的直接子元素时,
子元素()
可以为您带来当前元素的任何子元素。另一个选项是使用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 System.Xml.XPath
从方法样式切换到XPath很简单。这是有关XPath语法的基础知识:

  • 表达式前面的点(
    )指示从当前节点搜索。如果没有它,表达式将从根节点求值
  • 双斜杠(
    /
    )类似于
    子体()
  • 单斜杠(
    /
    )类似于
    Element()
    Elements()

  • 如何使用
    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
                }
    

    为什么不先存储rates和rate元素呢?非常感谢!我从来不知道Let函数(我相信它就是这样)的存在。记录在案,有没有关于LINQ到XML的好教程的站点?
    Let
    是一个关键字,通常是LINQ的一部分,因此您可能应该从LINQ教程开始,而不是从LINQ到XML的特定站点开始。
    Imports <xmlns:_ns="test">
    
    _rates.AddRange(query)