I have an XmlElement with several attributes
xeObject = <Object Name="Object1" Site="Site1" ... />
I also have a list of XmlElements with several (possibly different) attributes
l_xeObject = <Object ... /><Object ... /> ... <Object ... />
I would like a function call like
FindMatchingElements(xeObject, l_xeObject, "Name", "Site")
where any elements in l_xeObject that have the same values as xeObject.Name and xeObject.Site are returned!
Can i do this with Linq?
… I already have the following function
public static List<XmlElement> GetXmlElementsFromListWithMatchingAttribute
(XmlElement xeMatchOn, string sMatchingAttributeName, List<XmlElement> l_xeSearchIn)
{
return (l_xeSearchIn
.Where(xe => xe.Attributes[sMatchingAttributeName].Value
== xeMatchOn.Attributes[sMatchingAttributeName].Value)
).ToList();
}
but it only uses one attribute.
Thanks to Frédéric for the answer i needed.
You can use All() to match several attributes and GetAttribute() to avoid having to check for
null:That said, if you really want to parse or generate XML markup with LINQ, consider using LINQ to XML instead of the DOM classes.