2014-07-02 23 views
1

我試圖讓其中一個XML節點爲我提供其子元素的值,但是我擁有的解決方案(迄今爲止)顯示了我的孩子所有元素的元素。從單個XML節點中提取信息但沒有獲取其他孩子的值的兒童

這就是XML是

<?xml version="1.0"?> 
<fittings> 
    <fitting name="Caracal - Rapid light"> 
     <description value=""/> 
     <shipType value="Caracal"/> 
     <hardware slot="low slot 0" type="Damage Control II"/> 
     <hardware slot="low slot 1" type="Ballistic Control System II"/> 
     <hardware slot="low slot 2" type="Ballistic Control System II"/> 
     <hardware slot="low slot 3" type="Ballistic Control System II"/> 
     <hardware slot="med slot 0" type="Adaptive Invulnerability Field II"/> 
     <hardware slot="med slot 1" type="Large Shield Extender II"/> 
     <hardware slot="med slot 2" type="Large Shield Extender II"/> 
     <hardware slot="med slot 3" type="X5 Prototype Engine Enervator"/> 
     <hardware slot="med slot 4" type="Warp Disruptor II"/> 
     <hardware slot="hi slot 0" type="Rapid Light Missile Launcher II"/> 
     <hardware slot="hi slot 1" type="Rapid Light Missile Launcher II"/> 
     <hardware slot="hi slot 2" type="Rapid Light Missile Launcher II"/> 
     <hardware slot="hi slot 3" type="Rapid Light Missile Launcher II"/> 
     <hardware slot="hi slot 4" type="Rapid Light Missile Launcher II"/> 
     <hardware slot="rig slot 0" type="Medium Bay Loading Accelerator I"/> 
     <hardware slot="rig slot 1" type="Medium Anti-EM Screen Reinforcer I"/> 
     <hardware slot="rig slot 2" type="Medium Core Defense Field Extender I"/> 
    </fitting> 
    <fitting name="Caracal - heavy"> 
     <description value=""/> 
     <shipType value="Caracal"/> 
     <hardware slot="low slot 0" type="Damage Control II"/> 
     <hardware slot="low slot 1" type="Ballistic Control System II"/> 
     <hardware slot="low slot 2" type="Ballistic Control System II"/> 
     <hardware slot="low slot 3" type="Ballistic Control System II"/> 
     <hardware slot="med slot 0" type="Warp Disruptor II"/> 
     <hardware slot="med slot 1" type="X5 Prototype Engine Enervator"/> 
     <hardware slot="med slot 2" type="Large Shield Extender II"/> 
     <hardware slot="med slot 3" type="Large Shield Extender II"/> 
     <hardware slot="med slot 4" type="Small Shield Booster II"/> 
     <hardware slot="hi slot 0" type="Heavy Missile Launcher I"/> 
     <hardware slot="hi slot 1" type="Heavy Missile Launcher I"/> 
     <hardware slot="hi slot 2" type="Heavy Missile Launcher I"/> 
     <hardware slot="hi slot 3" type="Heavy Missile Launcher I"/> 
     <hardware slot="hi slot 4" type="Heavy Missile Launcher I"/> 
     <hardware slot="rig slot 0" type="Medium Capacitor Control Circuit I"/> 
     <hardware slot="rig slot 1" type="Medium Core Defense Field Extender I"/> 
     <hardware slot="rig slot 2" type="Medium Core Defense Capacitor Safeguard I"/> 
    </fitting> 
</fittings> 

可以有一個XML文檔中的許多不同元素的格式

我的代碼是試圖讓每個硬件孩子的「類型」一配件。我目前的代碼將檢索整個XML文檔中的每種類型,並且我無法將其隔離到其中一個節點。任何意見將非常感激:)

'Import XML data from slected file' 
    Dim ImportEntry As XElement = XElement.Load(Selected) 

    'value of selected fitting ("name" value) 
    Dim PropLook As String = ImportSelect.SelectedValue 

    'find and show fitted ship items 
    Dim hardware_type = ImportEntry...<hardware>.Attributes("type") 
    For Each Attribute In hardware_type 
     FitProporties.Items.Add(Attribute.Value) 
    Next 

回答

0

這是一種可能的方式,你可以嘗試:

Dim PropLook As String = ImportSelect.SelectedValue 

'filter <fitting> by its name attribute...' 
'then search all corresponding <hardware> child ...' 
'and get its type attribute value' 
Dim hardware_type = ImportEntry.<fittings>.<fitting>.Where(Function(x) [email protected] = PropLook).<hardware>.Attributes("type") 
For Each Attribute In hardware_type 
    FitProporties.Items.Add(Attribute.Value) 
Next 
+0

這並不是出於某種原因返回值。 :( – user3797758

+0

對我來說工作很好,使用帶有硬編碼'PropLook'字符串的XML樣本進行測試。調試您的應用程序並找出問題所在。 – har07