C# - XML,计算其他元素中特定元素的数量

时间:2016-05-22 16:11:08

标签: c# xml

我有一个XML,我将其加载到我的C#程序中。我想要实现的目标是计算军队的数量"每个"联盟内的元素"元件。

以下是每个"联盟"包含三个"军队"元素。因此,我想要实现的结果是有两个变量,结果为3。

<?xml version="1.0"?>
<battle>
  <alliance id="0">
    <army>
      <faction>factionName</faction>
      <unit>
        <unit_type type="unit_A" />
        <general>
          <commander_record_key>commanderName</commander_record_key>
        </general>
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_A" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_A" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
    </army>
    <army>
      <faction>factionName</faction>
      <unit>
        <unit_type type="unit_A" />
        <general>
          <commander_record_key>commanderName</commander_record_key>
        </general>
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_B" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_B" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
    </army>
    <army>
      <faction>factionName</faction>
      <unit>
        <unit_type type="unit_C" />
        <general>
          <commander_record_key>commanderName</commander_record_key>
        </general>
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_C" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_C" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
    </army>
    <victory_condition>
      <kill_or_rout_enemy />
    </victory_condition>
  </alliance>
  <alliance id="1">
    <army>
      <faction>factionName</faction>
      <unit>
        <unit_type type="unit_F" />
        <general>
          <commander_record_key>commanderName</commander_record_key>
        </general>
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_F" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_F" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
    </army>
    <army>
      <faction>factionName</faction>
      <unit>
        <unit_type type="unit_F" />
        <general>
          <commander_record_key>commanderName</commander_record_key>
        </general>
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_F" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_F" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
    </army>
    <army>
      <faction>factionName</faction>
      <unit>
        <unit_type type="unit_F" />
        <general>
          <commander_record_key>commanderName</commander_record_key>
        </general>
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_F" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
      <unit>
        <unit_type type="unit_F" />
        <unit_capabilities>
          <special_ability>abilityName</special_ability>
        </unit_capabilities>
      </unit>
    </army>
    <victory_condition>
      <kill_or_rout_enemy />
    </victory_condition>
  </alliance>
</battle>
你能帮忙吗?非常感谢

2 个答案:

答案 0 :(得分:2)

您可以Linq使用Xml并查询后代和元素,如下所示。

    XDocument doc = XDocument.Load(filename);


    var results = doc.Descendants("alliance")                                
                     .Select(g=> 
                             new 
                             {
                                 ID = g.Attribute("id").Value, 
                                 ArmyCount =   g.Elements("army").Count() 
                             });

选中此demo

答案 1 :(得分:1)

可以使用 XmlDocument SelectNodes 完成替代方法:

List<int> listCount = new List<int>();
XmlDocument docXMLs = new XmlDocument();
docXML.Load("path to file.xml");

XmlNodeList listAlliance = docXML.SelectNodes("Battle/Alliance");
foreach(XmlNode nodeAlliance in listAlliance )
{
    XmlNodeList listArmy = nodAlliance.SelectNodes("Army");
    listCount.Add(listArmy.Count);
}

请注意,我没有尝试过上述代码,可能有更有效的方法。如果答案可以改进,那么我感谢任何建设性的评论!