title: C# - XML, Counting the number of specific elements within other elements
body: <p>I have an XML which I'm loading into my C# program. What I'd like to achieve is to count the number of "army" elements within each "alliance" element.</p>
  
  <p>Here's an example where each "alliance" contains three "army" elements. So the result I'd like to achieve is to have two variables with the result of 3.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;battle&gt;
    &lt;alliance id="0"&gt;
      &lt;army&gt;
        &lt;faction&gt;factionName&lt;/faction&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_A" /&gt;
          &lt;general&gt;
            &lt;commander_record_key&gt;commanderName&lt;/commander_record_key&gt;
          &lt;/general&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_A" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_A" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
      &lt;/army&gt;
      &lt;army&gt;
        &lt;faction&gt;factionName&lt;/faction&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_A" /&gt;
          &lt;general&gt;
            &lt;commander_record_key&gt;commanderName&lt;/commander_record_key&gt;
          &lt;/general&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_B" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_B" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
      &lt;/army&gt;
      &lt;army&gt;
        &lt;faction&gt;factionName&lt;/faction&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_C" /&gt;
          &lt;general&gt;
            &lt;commander_record_key&gt;commanderName&lt;/commander_record_key&gt;
          &lt;/general&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_C" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_C" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
      &lt;/army&gt;
      &lt;victory_condition&gt;
        &lt;kill_or_rout_enemy /&gt;
      &lt;/victory_condition&gt;
    &lt;/alliance&gt;
    &lt;alliance id="1"&gt;
      &lt;army&gt;
        &lt;faction&gt;factionName&lt;/faction&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;general&gt;
            &lt;commander_record_key&gt;commanderName&lt;/commander_record_key&gt;
          &lt;/general&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
      &lt;/army&gt;
      &lt;army&gt;
        &lt;faction&gt;factionName&lt;/faction&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;general&gt;
            &lt;commander_record_key&gt;commanderName&lt;/commander_record_key&gt;
          &lt;/general&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
      &lt;/army&gt;
      &lt;army&gt;
        &lt;faction&gt;factionName&lt;/faction&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;general&gt;
            &lt;commander_record_key&gt;commanderName&lt;/commander_record_key&gt;
          &lt;/general&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
        &lt;unit&gt;
          &lt;unit_type type="unit_F" /&gt;
          &lt;unit_capabilities&gt;
            &lt;special_ability&gt;abilityName&lt;/special_ability&gt;
          &lt;/unit_capabilities&gt;
        &lt;/unit&gt;
      &lt;/army&gt;
      &lt;victory_condition&gt;
        &lt;kill_or_rout_enemy /&gt;
      &lt;/victory_condition&gt;
    &lt;/alliance&gt;
  &lt;/battle&gt;
  </code></pre>
  
  <p>Can you help? Many Thanks</p>