Answered filtering a List<XElement> on an attribute value

  • Wednesday, May 09, 2012 8:10 PM
     
     

    Assume I have a variable named List<XElement> trial.  The XElements have an attribute named "tokeep".  Some of these "tokeep" nodes have a value of "*" and the others have a blank value for the "tokeep" attribute.

    Using Linq, is there a way to filter trial to retain only those XElements which have a "*" in the "tokeep" attribute node?

All Replies

  • Thursday, May 10, 2012 8:51 AM
     
     Answered

    Yes,

      trial.Where(t => (string)t.Attribute("tokeep) == "*")

    should do or with the query syntax

      from t in trial

      where (string)t.Attribute("tokeep") == "*"

      select t

    You might want to make yourself familiar with the LINQ documentation and the LINQ to XML documentation.


    MVP Data Platform Development My blog

    • Proposed As Answer by Martin Honnen Thursday, May 10, 2012 11:25 AM
    • Marked As Answer by Newmanb1 Thursday, May 10, 2012 3:57 PM
    •