How to count number of XML nodes that contain specific value

34,659
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;

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

Share:
34,659
InsertOldUserIDHere
Author by

InsertOldUserIDHere

Come on, that is Batman on an Elephant!! SOreadytohelp

Updated on January 12, 2020

Comments

  • InsertOldUserIDHere
    InsertOldUserIDHere over 4 years

    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.

    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();
    

    Below is my 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>