Find XmlElement in List using array of matching attribute names … Using Linq?

You can use All() to match several attributes and GetAttribute() to avoid having to check for null : public static IEnumerable FindMatchingElements(XmlElement match, IEnumerable elements, params string attributeNames) { // Argument validation omitted for brevity. Return elements. Where( element => attributeNames.

All( name => element. GetAttribute(name) == match. GetAttribute(name))); } That said, if you really want to parse or generate XML markup with LINQ, consider using LINQ to XML instead of the DOM classes.

You can use All() to match several attributes and GetAttribute() to avoid having to check for null: public static IEnumerable FindMatchingElements(XmlElement match, IEnumerable elements, params string attributeNames) { // Argument validation omitted for brevity. Return elements. Where( element => attributeNames.

All( name => element. GetAttribute(name) == match. GetAttribute(name))); } That said, if you really want to parse or generate XML markup with LINQ, consider using LINQ to XML instead of the DOM classes.

Édéric, Thanks for your response. I had to change '. Attributesname' to '.Attributesname.

Value' to get it to work, but that means I now get an error if I specify an attribute in the params that is not present on the match element..?! – user597118 Jun 2 at 10:15 @user597118, you're absolutely right about . Value (I'm not used to XmlElement anymore, too much LINQ to XML I guess).

I updated my answer to use GetAttribute() instead, which will return the empty string if the attribute is not present. – Frédéric Hamidi Jun 2 at 10:25 édéric, Awesome thanks! It is nice to be right about something, just don't tell my missus :-) I am just reading up on Linq to Xml and can already see the advantages... thanks for the link and necessary kick up the a**e!

– user597118 Jun 2 at 10:32.

But it only uses one attribute. Thanks to Frédéric for the answer I needed.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions