views:

602

answers:

1

I have the following node:

<NodeA desc="Cheap Item 1" category="Cooking" />

I selected the 'category' attribute using the following:

.Where(attr => attr.Name == "category")
.Select(attr => attr.Value);

How can I select both the 'desc' and 'category' now

+1  A: 

Edit I think I was actually misunderstanding your original question. The code below should find the proper node in a list of nodes and select out it's desc and category properties.

var contents = nodes.Where( n => n.Name == "NodeA")
                    .Select( node => new { desc = node.Attribute("desc")
                                                      .Value,
                                          category = node.Attribute("category")
                                                          .Value
                                         }
                     );

var desc = contents.desc;
var category = contents.category;
tvanfosson
thanks, but this does not compile at my end..it says 'string' does not contain a definition for 'Attributes' and the best extension method overload '...
Yes. I think I misunderstood where you were starting from. I think I've corrected it based on a better understanding.
tvanfosson
Essentially, skip the iteration over all the attributes and get the named attributes out of the node itself after you've selected it by name. If you already have the node from some other query you could use var desc = node.Attribute("desc").Value; var category = node.Attribute("category").Value.
tvanfosson