使用VB.NET解析具有相同名称的节点的XML文件 [英] Parsing XML file with nodes of same name with VB.NET

查看:61
本文介绍了使用VB.NET解析具有相同名称的节点的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次遇到解析XML的问题.我已经弄清楚了几乎所有内容,但是被卡在我有多个同名节点的地方.这是XML的摘录

Once again having issues parsing XML. I've got almost all of it figured out but am stuck where I have multiple nodes with the same name. Here's a snippet from the XML

<HotelDetailsRsp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" TraceId="0.7055475" TransactionId="72CEA41F0A0758AA26AA4A14D780FC06" ResponseTime="1069">
  <RequestedHotelDetails xmlns="http://www.travelport.com/schema/hotel_v19_0">
    <HotelProperty HotelChain="LC" HotelCode="14645" HotelLocation="BOM" Name="ITC GRAND CENTRAL MUMBAI">
      <PropertyAddress>
        <Address>Dr Babasaheb Ambedkar Road</Address>
        <Address>Mumbai 400012 IN</Address>
        <Address>Parel</Address>
      </PropertyAddress>
      <PhoneNumber xmlns="http://www.travelport.com/schema/common_v17_0" Type="Business" Number="91 22-24101010"/>
      <PhoneNumber xmlns="http://www.travelport.com/schema/common_v17_0" Type="Fax" Number="91 22-24101111"/>
      <Distance xmlns="http://www.travelport.com/schema/common_v17_0" Value="6" Direction="S"/>
    </HotelProperty>
  </RequestedHotelDetails>
</HotelDetailsRsp>

这是我用来解析的VB.NET代码

And this is the VB.NET code I'm using to parse it with

For Each n As XElement In _xDoc.Descendants(_ns + "HotelProperty")
    _hotelProperty.Add(New HotelProperty With { _
                   .HotelChain = n.Attribute("HotelChain").Value, _
                   .HotelCode = n.Attribute("HotelCode").Value, _
                  .HotelLocation = n.Attribute("HotelLocation").Value, _
                  .HotelName = n.Attribute("Name").Value, _
                  .Address = n.Descendants(_ns + "PropertyAddress").Select(Function(el As String) el).ToList(), _
                  .PhoneNumber = n.Descendants(_ns + "PhoneNumber").Where(Function(e) e.Attribute("Type") = "Bunsiness").Value, _
                  .FaxNumber = n.Descendants(_ns + "PhoneNumber").Where(Function(e) e.Attribute("Type") = "Fax").Value})
Next

我测试除PhoneNumber和FaxNumber之外的所有值时,都会填充它.我将如何实现这一目标?谢谢

All values are populated when I test it except PhoneNumber and FaxNumber. How would I go about accomplishing this? Thanks

推荐答案

  1. PhoneNumber元素具有不同的名称空间

  1. PhoneNumber elements have difference namespace

Dim _ns2 = XNamespace.Get("http://www.travelport.com/schema/common_v17_0")

  • 您要查找的数字不是元素的值,而是存储在Number属性中

    .PhoneNumber = n.Descendants(_ns2 + "PhoneNumber").First(Function(e) e.Attribute("Type") = "Business").Attribute("Number").Value, _
    .FaxNumber = n.Descendants(_ns2 + "PhoneNumber").First(Function(e) e.Attribute("Type") = "Fax").Attribute("Number").Value
    

  • 这篇关于使用VB.NET解析具有相同名称的节点的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆