【问题标题】:To get the sum of values from Xml innertags of unique type, through XmlDocument通过 XmlDocument 从唯一类型的 Xml 内标签中获取值的总和
【发布时间】:2014-01-21 05:17:18
【问题描述】:

通过 XmlDocument,我想获得零件编号为“500300-B21”的所有数量的总和。我在 C# 中的代码是:-

XmlDocument xworkload = new XmlDocument();
private void BtnPnoCount_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    dlg.FileName = "Document"; // Default file name
    dlg.DefaultExt = ".hpa"; // Default file extension
    dlg.Filter = "Xml documents (.hpa)|*.hpa"; // Filter files by extension 

    var result = dlg.ShowDialog();

    if (result == true)
    {
        xworkload.Load(dlg.FileName);
        string xmlcontents = xworkload.InnerXml; // To get inner Xml

     if (null != xworkload.DocumentElement.SelectSingleNode("Interview"))
        {
            if (xworkload.DocumentElement.SelectSingleNode("Interview/Rack")!= null)
            {
                var xpath = "//Item/partnumber";

                XmlNodeList qtysum = xworkload.DocumentElement.SelectNodes(xpath);
                int sum = 0;

                foreach (XmlNode node in qtysum)
                {
                    if (node.SelectSingleNode("partnumber").InnerText == "500300-B21")
                    {
                        sum += Int32.Parse(node.SelectSingleNode("quantity").InnerText);
                    }
                    MessageBox.Show("The the sum of quantities with P/No 500300-B21 is " + sum.ToString());
                }

            }
        }
    }
}

这会引发 NullReferenceException。我想用那个零件号得到数量的总和

【问题讨论】:

  • 仅供参考,XmlDocument 是 .NET 的一部分,而不是 C# 的一部分
  • @JohnSaunders:谢谢,我已经更新了

标签: .net xml loops foreach nodes


【解决方案1】:

这对我有用。

static void Main(string[] args)
{
    // Load Doc
    var xworkload = new XmlDocument();
    xworkload.Load("C:\\temp\\xmldoc.xml");

    var sum = 0;
    // Loop through each processor/item node
    foreach (XmlNode node in xworkload.SelectNodes("//Processor/Item"))
    {
         // Check part number
         if (node.SelectSingleNode("partnumber").InnerText == "500300-B21")
         {
             // Add to sum
             sum += Int32.Parse(node.SelectSingleNode("quantity").InnerText);
         }
    }  
}

【讨论】:

  • @Dannyh,谢谢,它适用于处理器下的零件号。
  • 是的,您的原始帖子中有处理器,所有项目只需将 XmlNode node in xworkload.SelectNodes("//Processor/Item") 更改为 XmlNode node in xworkload.SelectNodes("//Item")
【解决方案2】:

使用LINQ to XML 更简单,试试这个:

XDocument xDoc = XDocument.Load(dlg.FileName);
var sum = xDoc.Descendants("item")
              .Where(item => (string)item.Element("partnumber") == "500300-B21")
              .Select(x => (int)x.Element("quantity"))
              .Sum();

或更安全的版本:

var sum = xDoc.Descendants("item")
              .Where(item => (string)item.Element("partnumber") == "500300-B21")
              .Select(x =>
              {
                  int result;
                  if (int.TryParse((string) x.Element("quantity"), out result)) 
                     return result;
                  else 
                     return 0;
              })
              .Sum();

【讨论】:

  • @MarcinJuraszek 你能建议一些参考这个 XmlDocument 的东西吗,我不能使用 LINQ to Xml。
  • @MishraSaurabh 我不使用XmlDocument。为什么不能使用 LINQ to XML?
【解决方案3】:

这也可以在单个 XPath 查询中完成

对于所有处理器数量的总和:

sum(//Processor/Item[partnumber='500300-B21']/quantity)

对于所有商品数量:

sum(//Item[partnumber='500300-B21']/quantity)

请注意,根据您的 xml,partnumber 是一个元素,而不是一个属性,因此请省略 @。 您的文档格式也不正确 (<?xml ...)

【讨论】:

    【解决方案4】:

    这是答案,它奏效了。

    if (null != xworkload.DocumentElement.SelectSingleNode("Interview"))
                        {
                            if (xworkload.DocumentElement.SelectSingleNode("Interview/Rack")!= null)
                            {
                                var xpath = "//Item";
    
                                XmlNodeList qtysum = xworkload.DocumentElement.SelectNodes(xpath);
                                int sum = 0;
    
                                foreach (XmlNode node in qtysum)
                                {
                                    if (node.SelectSingleNode("partnumber").InnerText == "500300-B21")
                                   {
                                       sum += Int32.Parse(node.SelectSingleNode("quantity").InnerText);
                                   }                           
                                }
    
                                MessageBox.Show("The the sum of quantities with P/No 500300-B21 is " + sum.ToString());
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      相关资源
      最近更新 更多