如何从C#中选定的TreeView节点读取特定字段?

时间:2017-02-01 19:07:11

标签: c# wpf treeview xmlnode

我将TreeView声明为:

 <TreeView Name="OOB"
 <!-- etc., -->

一个XML文件(代码段):

Army>
<ArmyName>The Army of Northern Virginia</ArmyName>
 <Commander> 
     <CommanderName>The Emperor Napoleon</CommanderName>
     <CommanderLeadership>94</CommanderLeadership> 
        <Division>
            <DivisionCommanderName>Major General William T. Sherman</DivisionCommanderName>
            <DivisionCommanderLeadership>78</DivisionCommanderLeadership>
            <Unit>
                <UnitName>Chasseurs à Cheval Garde</UnitName>
                <UnitIcon>Cavalry.png</UnitIcon>
                <UnitType>Cavalry</UnitType>
                <UnitKStrength>3</UnitKStrength>
                <UnitStrength>456</UnitStrength>
                <UnitQuality>94</UnitQuality>
                <UnitMorale>72</UnitMorale>
                <UnitLeadership>74</UnitLeadership>
                <UnitAmmunition>99</UnitAmmunition>
            </Unit>
            <Unit>
                <UnitName>Battery B 4th US Artillery</UnitName>
                <UnitIcon>Artillery.png</UnitIcon>
                <UnitType>Artillery</UnitType>
                <UnitKStrength>3</UnitKStrength>
                <UnitStrength>4456</UnitStrength>
                <UnitQuality>88</UnitQuality>
                <UnitMorale>65</UnitMorale>
                <UnitLeadership>82</UnitLeadership>
                <UnitAmmunition>25</UnitAmmunition>
            </Unit>
        </Division>
        <Division>
            <DivisionCommanderName>Spoons Butler</DivisionCommanderName>
            <DivisionCommanderLeadership>18</DivisionCommanderLeadership>
            <Unit>
                <UnitName>2nd Wisconsin Infantry</UnitName>
                <UnitIcon>Infantry.png</UnitIcon>
                <UnitType>Infantry</UnitType>
                <UnitKStrength>3</UnitKStrength>
                <UnitStrength>1456</UnitStrength>
                <UnitQuality>92</UnitQuality>
                <UnitMorale>48</UnitMorale>
                <UnitLeadership>87</UnitLeadership>
                <UnitAmmunition>25</UnitAmmunition>
            </Unit>
            <Unit>
                <UnitName>3rd Brigade Supply Train</UnitName>
                <UnitIcon>Supplies.png</UnitIcon>
                <UnitType>Supply</UnitType>
                <UnitKStrength>3</UnitKStrength>
                <UnitStrength>4256</UnitStrength>
                <UnitQuality>32</UnitQuality>
                <UnitMorale>36</UnitMorale>
                <UnitLeadership>25</UnitLeadership>
                <UnitAmmunition>3</UnitAmmunition>
            </Unit>

在TreeView中单击的项目存储在:

XmlNode selectedNode;
selectedNode = (XmlNode)OOB.SelectedItem;

我需要阅读该字段

<DivisionCommanderLeadership>

在所选节点中。例如,如果是上面的示例,则用户单击:

<DivisionCommanderName>Spoons Butler</DivisionCommanderName>

我想获得价值:     18

我之前提出过这个问题并被告知要使用:

 XmlNode node = xmlFile.SelectSingleNode(".//DivisionCommanderLeadership");

不幸的是,这总是返回DivisionCommanderLeadership的第一个实例,而不是选定节点中的字段(即在上面的示例中,对于Spoons Butler少将返回'78',而对于Spoons Butler则返回'18'。

请帮帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

必须是这样的:

XmlNode node = selectedNode.SelectSingleNode(".//DivisionCommanderLeadership");

您只想搜索所选元素,而不是整个xml。