如何在 C#
中使用 linq
获取XmlElement的属性而不是XElement的属性?
public string test (XmlElement element)
{
var enumAttr = from attr in element.Attributes select attr;
foreach (var data in enumAttr)
{
// TO DO
}
}
这是一个错误,
无法找到源类型“System.Xml.XmlAttributeCollection”的查询模式的实现 . 找不到“选择” . 考虑明确指定范围变量'attr'的类型
1 回答
这是因为
XmlAttributeCollection
只实现IEnumerable
而不是IEnumerable<T>
. 您只需将查询表达式更改为:这相当于:
但是你无论如何都没有在LINQ做任何事情 - 你可以使用: