【问题标题】:To get the count of innertags in Xml using XmlDocument使用 XmlDocument 获取 Xml 中的内部标签计数
【发布时间】:2014-02-13 16:55:34
【问题描述】:

我有这个 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 中的机架总数。我想显示 5 作为输出。

【问题讨论】:

  • 一定要XmlDocument吗,用XDocument比较轻量。您可以使用 xpath 表达式获取 XmlNodeList 并简单地计算其中的项目数。
  • @stevethethread :我试过了, var xpath = "//Interview/Rack"; XmlNodeList cnt = xworkload.DocumentElement.SelectNodes(xpath);整数总和 = 0; foreach (cnt 中的 XmlNode 节点) { sum += Int32.Parse(?); } MessageBox.Show("机架总数为:" + sum.ToString());
  • 你试过cnt.Count吗?
  • @MrClan :是的,我也试过这样: var xpath = "//Interview/Rack"; XmlNodeList cnt = xworkload.GetElementsByTagName("Rack");计数;整数总和 = 0; foreach (cnt 中的 XmlNode 节点) { sum += Int32.Parse(cnt.Count); } MessageBox.Show("机架总数为:" + sum.ToString());
  • 请参阅下面的编辑答案,该答案在您的问题中使用相同的代码。您无需将 count 解析为 int,或使用 ToString() 将 int 重新转换为字符串。

标签: .net xml foreach xmldocument


【解决方案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。

【讨论】:

  • 对于您的场景,您可以使用任何 xpath://Rack 或 //Interview/Rack 或 Interview/Rack 它应该都选择所需的节点。
【解决方案2】:

我试过了,终于成功了..

if(xworkload.DocumentElement.SelectSingleNode("Interview/Rack") !=null)
                    {

                        XmlNodeList cnt = xworkload.GetElementsByTagName("Rack");                                         

                        MessageBox.Show("The Total number of Racks are :" + cnt.Count.ToString());
                    }

【讨论】:

  • 当然,有很多方法可以做到这一点。
猜你喜欢
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多