tags:

views:

19

answers:

1

Hello, I am trying to query an XML file. Below query returns the first element in the sequence. Wondering how to get all elements in the sequence as a List. rsltQuest is of type List of XElement.

 
rsltQuest = doc1.Descendants(xmlns + "QUESTION")
                                 .Where(t => t.Attribute("ANSWER").Value == "no").ToList();`


Thanks for your advices. M

A: 

I see two issues but both should not cause the result to be a one element list (provided there are more than one QUESTION elements having an ANSWER attribute with value "no"):

  • You close one more parenthesis than you open.
  • You could get a null pointer exception if there is an QUESTION element having no ANSWER attribute.

So, are you sure the data contains more than one QUESTION with ANSWER="no"? Or maybe this is a namespace issue?

EDIT: Maybe you should try (string)(t.Attribute("ANSWER")) == "no"

Frank
@Frank Thanks. Parenthesis is a typo. Yes my XML file has more than one QUESTION with ANSER = "no". I am sure it is not an namespace issue.
BumbleBee
Then you would have to narrow it down: What is the result from just `doc1.Descendants(xmlns + "QUESTION")`?
Frank
gives me all the QUESTION elements in the doc1
BumbleBee
So the issue must be in the Where() or the ToList() (or your check of the result. I just would try these systematically to narrow the area of the failure: If you add the Where, is the result still as expected? If yes, if you add the ToList()?
Frank