zetar
zetar

Reputation: 1233

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

I have a TreeView declared as:

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

And an XML file (snippet):

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>

The item that was clicked on in the TreeView is stored in:

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

And I need to read the field

<DivisionCommanderLeadership>

In the selected node. For example, if the above example, the user clicks on:

<DivisionCommanderName>Spoons Butler</DivisionCommanderName>

And I want to get the value: 18

I posed this question previously and was told to use:

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

Unfortunately this ALWAYS return the first instance of DivisionCommanderLeadership and not the field in the selected node (i.e. in the above example, it returns '78' for Major General William T. Sherman and not '18' for Spoons Butler.

A little help, please? Thanks!

Upvotes: 0

Views: 39

Answers (1)

Michael
Michael

Reputation: 2446

It must be something like that:

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

You want to search only on selected element, and not on the whole xml.

Upvotes: 1

Related Questions