再一次解析XML问题。我已经弄清楚了几乎所有的问题,但是我遇到了多个具有相同名称的节点。这是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代码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之外,我测试时会填充所有值。我将如何实现这一目标?感谢
答案 0 :(得分:2)
对我来说很明显的两件事:
Function(e) e.Attribute("Type") = "Bunsiness"
应该是
Function(e) e.Attribute("Type") = "Business"
并且_ns + "PhoneNumber"
应为_ns17 + "PhoneNumber"
,其中_ns17指向命名空间http://www.travelport.com/schema/common_v17_0
而不是http://www.travelport.com/schema/hotel_v19_0
答案 1 :(得分:1)
PhoneNumber
元素具有不同的命名空间
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