从单个 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

这是你可以尝试的一种方法:

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) x.@name = PropLook).<hardware>.Attributes("type")
For Each Attribute In hardware_type
    FitProporties.Items.Add(Attribute.Value)
Next