瞭解XML innertags的計數我有這樣的XML,它擁有5架要使用XmlDocument的
<?xml version="1.0" encoding="utf-8"?>
<workload>
<Interview>
<Rack Path="\Left_Rack\36 U.gif" Name="36" LName="36" Height="36" Selected="True" ID="8075" partnumber="AF046A" description="ABC Rack" />
<Rack Path="\Left_Rack\42 U.gif" Name="42" LName="42" Height="42" Selected="False" ID="3185" partnumber="AS0912" description="DEF Rack" />
<Rack Path="\Left_Rack\48 U.gif" Name="48" LName="48" Height="48" Selected="False" ID="7580" partnumber="AS0912" description="DEF Rack" />
<Rack Path="\Left_Rack\52 U.gif" Name="52" LName="52" Height="52" Selected="False" ID="3892" partnumber="AS0912" description="DEF Rack" />
<Rack Path="\Left_Rack\65 U.gif" Name="56" LName="56" Height="56" Selected="False" ID="9187" partnumber="AS0912" description="DEF Rack" />
</Interview>
</workload>
我在的XmlDocument加載XML文件,我想從上面陳列架總數XML。我想顯示5作爲輸出。
是否必須XmlDocument的,使用的XDocument更輕盈。您可以使用xpath表達式來獲取XmlNodeList,並簡單計算其中的項目數。 – stevethethread
@stevethethread:我試過了,var xpath =「// Interview/Rack」; XmlNodeList cnt = xworkload.DocumentElement.SelectNodes(xpath); int sum = 0; (cint中的XmlNode節點) sum + = Int32.Parse (「Racks總數:」+ sum.ToString()); – user42067
您是否嘗試過cnt.Count? – MrClan