2014-02-17 72 views
1

解析XML時再次出現問題。我已經想到了幾乎所有的東西,但是我堅持使用同一名稱的多個節點。下面是從XML使用VB.NET解析具有相同名稱的節點的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 

當我測試它除了******中國和FaxNumber所有值都被填充。我將如何去完成這件事?由於

回答

1
  1. PhoneNumber要素有差異的命名空間

    Dim _ns2 = XNamespace.Get("http://www.travelport.com/schema/common_v17_0") 
    
  2. 你想找的數量沒有元素的值,它存儲在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 
    
+0

謝謝@MarcinJuraszek,這就是訣竅。 – PsychoCoder

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_0http://www.travelport.com/schema/hotel_v19_0

+0

製造這些變化,並從返回沒什麼好去爲電話和傳真號碼返回空字符串。有些東西告訴我這是在函數(e)e.Attribute =「Business」部分,但我不能完全把它放在它上面 – PsychoCoder

相關問題