I have a XML where I have several <testcase>
tags. Some of them have attribute "ident" with value "pr", some not.
<testcase>
<teststep ident="Preparation" result="na">blabla</teststep>
<teststep ident="" result="pass">blabla</teststep>
<teststep ident="-" result="na">blabla</teststep>
<teststep ident="Info" result="na">blabla</teststep>
<teststep ident="1" result="pass">blabla</teststep>
<teststep ident="2" result="pass">blabla</teststep>
<teststep ident="3" result="pass">blabla</teststep>
<teststep ident="4" result="fail">blabla</teststep>
<teststep ident="PR" result="na">blabla</teststep>
<verdict result="fail" />
</testcase>
I would like to query testcases which contains ident attribute with value "pr"
IEnumerable<XElement> failedPRTCs = report.Descendants("testcase").Where(t => t.Element("verdict").Attribute("result").Value == "fail" && t.Descendants("teststep").Where(ts=> ts.Attribute("ident").Value == "pr").ToList().Count > 0).ToList();
Current query returns 0. Whats the problem?
Looks like a simple problem of casing! "pr"
vs "PR"
! I would also recommend using Any
rather than count > 0
:
IEnumerable<XElement> failedPRTCs = report.Descendants("testcase")
.Where(t => t.Element("verdict").Attribute("result").Value == "fail" &&
t.Descendants("teststep").Any(ts=> ts.Attribute("ident").Value == "PR"));