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?).

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

Thanks!

Jon Skeet
people
quotationmark

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 :)

people

See more on this question at Stackoverflow