How to get the attributes of XmlElement rather than XElement in C#
with linq
?
public string test (XmlElement element)
{
var enumAttr = from attr in element.Attributes select attr;
foreach (var data in enumAttr)
{
// TO DO
}
}
It's giving an error,
Could not find an implementation of the query pattern for source type 'System.Xml.XmlAttributeCollection'. 'Select' not found. Consider explicitly specifying the type of the range variable 'attr'
This is because XmlAttributeCollection
only implements IEnumerable
rather than IEnumerable<T>
. You can just change your query expression to:
var enumAttr = from XmlAttribute attr in element.Attributes select attr;
which is equivalent to:
var enumAttr = from attr in element.Attributes.Cast<XmlAttribute>() select attr;
But you're not really doing anything with the LINQ here anyway - you can just use:
foreach (XmlAttribute data in enumAttr.Attributes)
See more on this question at Stackoverflow