I trying to get all attribute values from xml file where value doesn’t start with some text
i have this code
IEnumerable<XElement> elements =
(from el in xmlFile.Root.Elements(elementName)
where (string)el.Attribute(attributeName) !StartWith("abc")
select el);
How can i fix this
You need to use a valid expression, e.g.
It’s important to understand that what goes into a LINQ
whereclause isn’t “magic” syntax – it’s just a normal expression, which can use the range variables declared by the query expression (elin this case). So you should have asked yourself, “If I were writing this not in LINQ, and I had a variable calledelreferring to an element, how would I have written anifcondition to check that the attribute value didn’t start withabc?”(I use the explicit conversion when the attribute can be missing and I want to just get a null, but in this case you’re going to go bang when the attribute’s missing anyway, and you just want the string value, so you might as well use the
Valueproperty.)Note that as you’ve only got a
whereclause here (and a trivialselect) it would probably be more readable to use the non-query-expression form: