2011-01-10 12 views
4

"いいえ"という値と要素の合計数を含むXMLファイル内のノードを数える方法を探しています。特定の値を含むXMLノードの数をカウントする方法

私は要素の数が問題なく動作していますが、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

+0

'NoCount'はおそらく' int'の代わりに 'XmlNodeList'でしょう。あるいは、 'readDoc.SelectNodes(" JSEnabled [。= \ "No \"] ")。Count'を使うべきです。 – Brian

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

は私のXMLです:

は、私が使用しています合計数を取得するには私は、XPathで「いいえ」

の 値を含むXMLファイル内 ノードをカウントする方法を探しています

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

などの合計数 要素。

count(/root/User) 

またはノードセットの結果メンバーをカウントするためのノードを選択するための表現と任意のDOMメソッドを使用します。あなたはすでにそれを持っている

関連する問題