0

I really need help with the where clause below.

Consider this xml

<name xmlns="http://www.imsglobal.org/xsd/imslip_v1p0">  
<partname>  
  <typename>  
    <tyvalue>First</tyvalue>  
  </typename>  
  <text>Charles</text>
</partname>  
<partname>  
   <typename>  
       <tyvalue>Last</tyvalue>  
    </typename>  
    <text>Ward</text>  
  </partname>  
</name>

This code returns one element

List<XElement> xElements = xmlDocument.Descendants(Aw + "name").
Where(x => (x.Element(Aw + "partname").Element(Aw + "typename").Element(Aw + "tyvalue")).Value == "First").
Select(x => x.Element(Aw + "partname").Element(Aw + "text")).ToList();

But if I change the where clause to "Last", it returns zero elements.

List<XElement> xElements = xmlDocument.Descendants(Aw + "name").
Where(x => (x.Element(Aw + "partname").Element(Aw + "typename").Element(Aw + "tyvalue")).Value == "Last").
Select(x => x.Element(Aw + "partname").Element(Aw + "text")).ToList();

Please help. I'm really stuck.

FatAlbert
  • 4,890
  • 6
  • 22
  • 34

2 Answers2

0

Try this:

List<XElement> xElements = xmlDocument.Descendants(Aw + "name")
                .Descendants(Aw + "partname")
                .Descendants(Aw + "typename")
                .Descendants(Aw + "tyvalue")
                .Where(x => x.Value == "Last")
                .ToList();
Chandu
  • 81,493
  • 19
  • 133
  • 134
0

x.Element returns the first child; since has many , you would need to use "Elements" instead of "Element", upon which you must iterate.