我有这个Xml,它有5个架子
<?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中显示Racks的总数。我想显示5作为输出。
答案 0 :(得分:1)
xworkload.SelectNodes("//Interview/Rack").Count
使用您的代码,如果只需要显示Rack节点的数量:
XmlDocument xworkload = new XmlDocument();
private void btnTotalRack_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".hpa";
dlg.Filter = "Xml document (.hpa)|*.hpa";
var result = dlg.ShowDialog(); //Opens the dialog box to select the xml file
if (result == true)
{
try
{
xworkload.Load(dlg.FileName); //load the Xml file in XmlDocument
MessageBox.Show("The Total number of Racks are :" + xworkload.SelectNodes("//Interview/Rack").Count);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}
这应该在弹出窗口中显示输出为5。
答案 1 :(得分:0)
我试过这个,最后它的工作......
if(xworkload.DocumentElement.SelectSingleNode("Interview/Rack") !=null)
{
XmlNodeList cnt = xworkload.GetElementsByTagName("Rack");
MessageBox.Show("The Total number of Racks are :" + cnt.Count.ToString());
}