如何计算包含特定值的XML节点的数量 [英] How to count number of XML nodes that contain specific value

查看:54
本文介绍了如何计算包含特定值的XML节点的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何计算XML文件中包含否"值以及元素总数的节点.

I am looking for how to count the nodes in an XML file that contain a value of "No" as well as the total number of elements.

我的元素计数工作正常,但是我不确定在XML内部查找要计数的逻辑的原因.

I have the element count working fine, but I am not sure of the logic to look inside the XML for a value to count.

要获取我正在使用的总数:

To get the total count I am using:

    XmlDocument readDoc = new XmlDocument();
    readDoc.Load(MapPath("Results.xml"));
    int count = readDoc.SelectNodes("root/User").Count;
    lblResults.Text = count.ToString();

下面是我的XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
  <User>
    <URL>http://www.example.com</URL>
    <JSEnabled>Yes</JSEnabled>
  </User>
  <User>
   <URL>http://www.example.com</URL>
   <JSEnabled>Yes</JSEnabled>
 </User>
 <User>
   <URL>http://www.example.com</URL>
   <JSEnabled>Yes</JSEnabled>
 </User>
 <User>
   <URL>http://www.example.com</URL>
   <JSEnabled>Yes</JSEnabled>
 </User>
 <User>
   <URL>http://www.example.com</URL>
   <JSEnabled>No</JSEnabled>
 </User>

推荐答案

XmlDocument readDoc = new XmlDocument();
readDoc.Load(MapPath("Results.xml"));
int count = readDoc.SelectNodes("root/User").Count;
lblResults.Text = count.ToString();
int NoCount = readDoc.SelectNodes("JSEnabled[. = \"No\"]").Count;

此处的参考文献很好: http://msdn.microsoft.com/zh-我们/library/ms256086.aspx

Good reference here: http://msdn.microsoft.com/en-us/library/ms256086.aspx

这篇关于如何计算包含特定值的XML节点的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆