如何获取所选节点的子节点中的某些字段。在C#中

时间:2017-01-31 16:34:04

标签: c# xml xmlnode

我曾经知道如何做到这一点,但我变老了。我有一个xml文件:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="divTable">
  <div class="divTableBody">
    <div class="divTableRow divTableHeading">
      <div class="divTableCell">+</div>
      <div class="divTableCell">00</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">16</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">32</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">48</div>
<div id="special">
      <div class="divTableCell">30</div>
      <div class="divTableCell">31</div>
      <div class="divTableCell">32</div>
      <div class="divTableCell">33</div>
</div>
      <div class="divTableCell">34</div>
      <div class="divTableCell">35</div>
      <div class="divTableCell">36</div>
      <div class="divTableCell">37</div>
      <div class="divTableCell">38</div>
      <div class="divTableCell">39</div>
      <div class="divTableCell">3A</div>
      <div class="divTableCell">3B</div>
      <div class="divTableCell">3C</div>
      <div class="divTableCell">3D</div>
      <div class="divTableCell">3E</div>
      <div class="divTableCell">3F</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell divTableHeading">0</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
    <div class="divTableRow endrow">
      <div class="divTableCell divTableHeading">256</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
      <div class="divTableCell">&nbsp;</div>
    </div>
  </div>
</div>

给定一个选定的节点(保证是一个Division节点):

<?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子节点的UnitName列表?

谢谢!请随意鄙视我的秃头。

2 个答案:

答案 0 :(得分:1)

你应该选择

var unitNames = selectedNode.SelectNodes(".//UnitName");
foreach(XmlElement unitName in unitNames)
{
    Console.WriteLine(unitName.InnerText);
}

答案 1 :(得分:0)

试试这个:

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);
}