简体   繁体   中英

Implicit conversion issue with XmlElement ad XmlNodeList

I have a Xml document that I want to convert into a XnlNodeList using a linq query. Now, neither Xml nor Linq are something know well. The error I'm getting is Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<System.Xml.XmlElement>' to 'System.Xml.XmlNodeList'. An explicit conversion exists (are you missing a cast?) Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<System.Xml.XmlElement>' to 'System.Xml.XmlNodeList'. An explicit conversion exists (are you missing a cast?) .

XmlNodeList abTestDocx = abTestDoc.GetElementsByTagName("FS").OfType<XmlElement>().OrderBy(FS => FS.GetAttribute("label"));

Thanks!

You don't generally create XmlNodeList instances yourself. Do you really need one though? If you just need to iterate over the nodes, just assign it to an IEnumerable<XmlElement> :

IEnumerable<XmlElement> abTestDocx = abTestDoc
     .GetElementsByTagName("FS")
     .OfType<XmlElement>()
     .OrderBy(fs => fs.GetAttribute("label"));

Note that using LINQ to XML is generally nicer than the old XmlDocument API. Then you'd just need:

IEnumerable<XElement> abTestDocx = doc
     .Descendants("FS")
     .OrderBy(fs => (string) fs.Attribute("label"));

... and all kinds of other things would be simpler too. LINQ to XML is lovely :)

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