2011-01-10 86 views
4

我正在寻找如何统计XML文件中包含值“否”以及元素总数的节点。如何统计包含特定值的XML节点数

我有元素计数工作正常,但我不确定的逻辑来查看XML的内部计数值。

要获得总计数我使用:

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> 

回答

7
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/en-us/library/ms256086.aspx

+0

这让我了解了我所要求的内容,但在阅读完参考链接后,我得到了更多的信息,然后我就开始寻找。非常感谢 – InsertOldUserIDHere 2011-01-10 20:24:47

1

我寻找如何计算在一个XML文件中包含的“否”

一个 值在XPath的 节点:

count(/root/User[JSEnabled = 'No']) 

以及总数 元素。

那你已经拥有了它:

count(/root/User) 

或者使用表达式选择的节点和任何DOM方法来计算节点集的成员。

相关问题