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

How do I read a specific field from a selected TreeView Node in C#?

我有一个 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");

不幸的是,这总是 return DivisionCommanderLeadership 的第一个实例,而不是所选节点中的字段(即在上面的示例中,它 return 是 William T. Sherman 少将的“78”而不是 Spoons Butler 的“18”。

请帮忙一点?谢谢!

一定是这样的:

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

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