2011-01-10 59 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方法來計算節點集的成員。

相關問題