0

I am unable to retrieve correct count of the nodes with attribute: IsTrue = "true" of my xml.

Code that I am using is :>>>

       Array depCnfg = (from node in xml.Descendants(Qualities).Descendants()
                           where node.Parent.Name == Qualities &&
                           (node.Attribute(ISTRUE).Value.ToString().ToLower() == "true") 
                           select { 
                           totalCount = node.Nodes().Count()}).ToArray();

XML USED: >>>

<Qualities>
 <General>
   <Tag1 IsTrue = "true">
      <SubTag1>0</SubTag1>
   </Tag1>    
   <Tag2 IsTrue = "false">
      <SubTag2>0</SubTag2>
   </Tag2>  
 </General>
</Qualities> 

Here I always get the count as 2 but actually it should be 1. If anyone have an idea please share...Thanks

4
  • Is depCnfg.Count == 2 or defCnfg[0].totalCount == 2? Commented Mar 13, 2012 at 18:02
  • @Rich: the totalCount inside 'select' is having a value of 2; ie, in the xml file both <Tag1> and <Tag2> are read, which is improper. I tried using lambda expression inside Count() but its not working. Commented Mar 13, 2012 at 18:55
  • You still count all nodes with node.Nodes() , not the nodes where IsTrue == "true". Commented Mar 14, 2012 at 12:34
  • @GertArnold: can you suggest me how to achieve it... Commented Mar 15, 2012 at 7:08

1 Answer 1

1

You can use the following LINQ query to get the count. This is grabbing the 'Qualities' node and putting it into it's own element. Then we are looking at each descendant, and check the following:

  1. Does it contain attributes
  2. Does it contain an attribute named 'IsTrue'
  3. Does the 'IsTrue' attribute's value equal 'true'

Then it gets the count of the elements that meet that criteria. This is assuming there is only one 'Qualities' node. In that case you would have to use XElement qualities = element.Element*s*("Qualities"); .

string sampleString = "<MAINROOT>" +
                        "<Qualities>" +
                         "<General>" +
                           "<Tag1 IsTrue = \"true\">" +
                              "<SubTag1>0</SubTag1>" +
                           "</Tag1>" +
                           "<Tag2 IsTrue = \"false\">" +
                              "<SubTag2>0</SubTag2>" +
                           "</Tag2>" +
                         "</General>" +
                        "</Qualities>" +
                    "</MAINROOT>";
        XElement element = XElement.Parse(sampleString);
        XElement qualities = element.Element("Qualities");
        if (qualities != null)
        {
            int trueCount = element.Element("Qualities")
            .Descendants()
            .Where(x => x.HasAttributes && x.Attribute("IsTrue") != null && x.Attribute("IsTrue").Value == "true")
            .Count();
        }
Sign up to request clarification or add additional context in comments.

1 Comment

I am glad! If you found this helpful, would you consider choosing the answer as the accepted answer, and uptick my response? This will close the question and also give me some credit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.