【问题标题】:Parse XML elements into List(Of String) with VB.NET使用 VB.NET 将 XML 元素解析为 List(Of String)
【发布时间】:2014-02-11 19:09:53
【问题描述】:

我从 Web 服务调用返回了一个巨大的 XML 文件,并且正在对其进行解析。我遇到的一个问题是将多个元素(具有相同名称)解析为 List(Of String)。 XML 看起来像这样

<BasicPropertyInfo ChainCode="CY" GEOConfidenceLevel="3" HotelCityCode="KWI" HotelCode="0048191" HotelName="COURTYARD KUWAIT CITY MARRIOTT" Latitude="29.377052" Longitude="47.990384" NumFloors="21" RPH="001">
  <Address>
    <AddressLine>AL SHUHADA ST PO BOX 1216 DASM</AddressLine>
    <AddressLine>DASMAN KUWAIT CITY KW 15463</AddressLine>
    <CountryCode>KW</CountryCode>
  </Address>
  <Award Provider="NTM3 CROWN" />
  <ContactNumbers>
    <ContactNumber Phone="965-22997000" Fax="965-22997001" />
  </ContactNumbers>

我的课很简单

Namespace Classes.Models
    Public Class PropertyInfo
        Public Property ChainCode() As String
        Public Property HotelCityCode() As String
        Public Property HotelCode() As String
        Public Property HotelName() As String
        Public Property Address() As IEnumerable(Of String)
        Public Property PhoneNumber() As String
        Public Property FaxNumber() As String
    End Class
End Namespace

在下面的代码中,如果我删除了我试图获取 AddressLine 的位置,它会填充类,只要我添加代码,它就会以错误

无法转换类型的对象 'd__142[System.Xml.Linq.XElement,System.Char]' to type 'System.Collections.Generic.IEnumerable1[System.String]'。

Public Function ParsePropertyInfo() As IEnumerable(Of PropertyInfo)
    Try
        Dim ns As XNamespace = "http://webservices.sabre.com/sabreXML/2003/07"
        Dim prop As PropertyInfo

        For Each n As XElement In _xDoc.Descendants(ns + "BasicPropertyInfo")
            _properties.Add(New PropertyInfo With { _
                             .ChainCode = n.Attribute("ChainCode").Value, _
                             .HotelCityCode = n.Attribute("HotelCityCode").Value, _
                             .HotelCode = n.Attribute("HotelCode").Value, _
                             .HotelName = n.Attribute("HotelName").Value,
                             .PhoneNumber = n.Element(ns + "ContactNumbers").Element(ns + "ContactNumber").Attribute("Phone").Value, _
                              .Address = n.Descendants(ns + "AddressLine").SelectMany(Function(el As String) el), _
                             .FaxNumber = n.Element(ns + "ContactNumbers").Element(ns + "ContactNumber").Attribute("Fax").Value})


        Next

        Return _properties
    Catch ex As Exception
        ErrorMessage = ex.Message
        Return Nothing
    End Try
End Function

那么我将如何将 AddressLine 元素添加到我的通用列表中?

【问题讨论】:

    标签: xml vb.net linq-to-xml


    【解决方案1】:

    使用Select 代替SelectMany

    .Address = n.Descendants(ns + "AddressLine").Select(Function(el As String) el)
    

    SelectMany 查看您的IEnumerable(Of XElement),将每个XElement 转换为string,然后将这些字符串中的所有字符合并为一个char 的巨大集合。那是因为string 实现了IEnumerable(Of char)

    您可能应该在Select 之后添加ToList() 调用,以确保您只存储string 实例的集合,而不是查询定义,每次访问该属性时都会执行该定义。

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 2014-04-05
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多