简体   繁体   English

如何在XxmlElement而不是C#中的XElement上运行linq

[英]how to run linq on XxmlElement rather than XElement in C#

How to get the attributes of XmlElement rather than XElement in C# with linq ? 如何使用linqC#获取XmlElement而不是XElement的属性?

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'. 找不到源类型“ System.Xml.XmlAttributeCollection”的查询模式的实现。 'Select' not found. 找不到“选择”。 Consider explicitly specifying the type of the range variable 'attr' 考虑明确指定范围变量“ attr”的类型

This is because XmlAttributeCollection only implements IEnumerable rather than IEnumerable<T> . 这是因为XmlAttributeCollection仅实现IEnumerable而不是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: 但是无论如何,您在这里实际上并没有对LINQ做任何事情-您可以使用:

foreach (XmlAttribute data in enumAttr.Attributes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM