如何获取所选 node.in C# 的子节点中的某些字段
How to get certain fields in child nodes of a selected node.in C#
我以前知道怎么做,但我变老了,变老了。我有一个 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<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>
</Division>
<Division>
<DivisionCommanderName>Advanced Guard</DivisionCommanderName>
<DivisionCommanderLeadership>86</DivisionCommanderLeadership>
<Unit>
<UnitName>27ème Régiment d'lnfanterie Légère</UnitName>
<UnitIcon>LightInfantry.png</UnitIcon>
<UnitType>Light Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>82</UnitQuality>
<UnitMorale>56</UnitMorale>
<UnitLeadership>75</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
</Division>
<Division>
<DivisionCommanderName>General of Division Olivier Rivaud de la Raffinière</DivisionCommanderName>
<DivisionCommanderLeadership>76</DivisionCommanderLeadership>
<Unit>
<UnitName>8ème Régiment d'lnfanterie de Ligne</UnitName>
<UnitIcon>Infantry.png</UnitIcon>
<UnitType>Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
<Unit>
<UnitName>45ème Régiment d'lnfanterie de Ligne</UnitName>
<UnitIcon>Infantry.png</UnitIcon>
<UnitType>Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
<Unit>
<UnitName>54ème Régiment d'lnfanterie de Ligne</UnitName>
<UnitIcon>Infantry.png</UnitIcon>
<UnitType>Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
<Unit>
<UnitName>2ème Régiment de Hussards</UnitName>
<UnitIcon>LightCavalry.png</UnitIcon>
<UnitType>Light Cavalry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>99</UnitAmmunition>
</Unit>
</Division>
</Commander>
</Army>
给定一个选定的节点(保证是分区节点):
selectedNode = (XmlNode)OOB.SelectedItem;
如何return selectedNode 的子节点的 UnitName 列表?
谢谢!请随意鄙视我的光头
你应该选择
var unitNames = selectedNode.SelectNodes(".//UnitName");
foreach(XmlElement unitName in unitNames)
{
Console.WriteLine(unitName.InnerText);
}
试试这个:
XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");
var divisions = doc.SelectNodes("//Division");
var firstDivision = divisions[0]; //your "SelectedItem"
XmlNodeList unitNames = firstDivision.SelectNodes("./Unit/UnitName");
foreach(XmlNode unitName in unitNames)
{
Console.WriteLine(unitName.InnerText);
}
我以前知道怎么做,但我变老了,变老了。我有一个 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<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>
</Division>
<Division>
<DivisionCommanderName>Advanced Guard</DivisionCommanderName>
<DivisionCommanderLeadership>86</DivisionCommanderLeadership>
<Unit>
<UnitName>27ème Régiment d'lnfanterie Légère</UnitName>
<UnitIcon>LightInfantry.png</UnitIcon>
<UnitType>Light Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>82</UnitQuality>
<UnitMorale>56</UnitMorale>
<UnitLeadership>75</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
</Division>
<Division>
<DivisionCommanderName>General of Division Olivier Rivaud de la Raffinière</DivisionCommanderName>
<DivisionCommanderLeadership>76</DivisionCommanderLeadership>
<Unit>
<UnitName>8ème Régiment d'lnfanterie de Ligne</UnitName>
<UnitIcon>Infantry.png</UnitIcon>
<UnitType>Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
<Unit>
<UnitName>45ème Régiment d'lnfanterie de Ligne</UnitName>
<UnitIcon>Infantry.png</UnitIcon>
<UnitType>Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
<Unit>
<UnitName>54ème Régiment d'lnfanterie de Ligne</UnitName>
<UnitIcon>Infantry.png</UnitIcon>
<UnitType>Infantry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>23</UnitAmmunition>
</Unit>
<Unit>
<UnitName>2ème Régiment de Hussards</UnitName>
<UnitIcon>LightCavalry.png</UnitIcon>
<UnitType>Light Cavalry</UnitType>
<UnitKStrength>2</UnitKStrength>
<UnitStrength>1256</UnitStrength>
<UnitQuality>94</UnitQuality>
<UnitMorale>78</UnitMorale>
<UnitLeadership>72</UnitLeadership>
<UnitAmmunition>99</UnitAmmunition>
</Unit>
</Division>
</Commander>
</Army>
给定一个选定的节点(保证是分区节点):
selectedNode = (XmlNode)OOB.SelectedItem;
如何return selectedNode 的子节点的 UnitName 列表?
谢谢!请随意鄙视我的光头
你应该选择
var unitNames = selectedNode.SelectNodes(".//UnitName");
foreach(XmlElement unitName in unitNames)
{
Console.WriteLine(unitName.InnerText);
}
试试这个:
XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");
var divisions = doc.SelectNodes("//Division");
var firstDivision = divisions[0]; //your "SelectedItem"
XmlNodeList unitNames = firstDivision.SelectNodes("./Unit/UnitName");
foreach(XmlNode unitName in unitNames)
{
Console.WriteLine(unitName.InnerText);
}