简体   繁体   中英

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 ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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