BumbleBee
BumbleBee

Reputation: 10809

Retrieve All Elements as a list - LINQ to XML

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

Upvotes: 0

Views: 362

Answers (1)

Frank
Frank

Reputation: 2648

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"

Upvotes: 1

Related Questions