开发者

Retrieve All Elements as a list - LINQ to XML

开发者 https://www.devze.com 2023-01-12 07:19 出处:网络
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.

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开发者_JS百科.Attribute("ANSWER").Value == "no").ToList();`


Thanks for your advices. M


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消