user256034
user256034

Reputation: 4369

linq to xml - remove elements

I'd like to remove all elements of type Item with an attribute view which has not a certain value.

So if the value is XXX. Select all elements Item which have an attribute view of value != XXX.

Some of the Item elements don't have a view attribute. This elements shouldn't be selected.

Upvotes: 1

Views: 1845

Answers (1)

Alex
Alex

Reputation: 35399

string selector = "XXX";

Elements.Where(x => x.Name == "Item"
                 && x.Attribute("view") != null
                 && x.Attribute("view").Value != selector).Remove();

Upvotes: 4

Related Questions