Reputation: 21
In C#, I sorted an RSS feed using XpathExpression and a self defined compare function for the field Publication Date.
I need to convert this sorted XPathNodeIterator
object to a List<XElement>
'Generic Linq type' object. I've tried to use an intermediary data-type but did not have any luck.
Tried:
List<XElement> elements = new List<XElement>();
IEnumerable<XElement> sortedElements;
IEnumerable<XElement> newElements;
sortedElements = (IEnumerable <XElement>) iterator;
I've also tried using order by on the list but that was unsuccessful using only List<XElement>
.
Upvotes: 1
Views: 2421
Reputation: 630
I know this question is old, but have you checked whether this works?
var xmlStrings = iterator.OfType<XPathNavigator>().Select(n => n.OuterXml).ToList();
var xmlElements = new List<XElement>();
xmlStrings.ForEach(el => xmlElements.Add(XElement.Parse(el)));
Upvotes: 0
Reputation: 78840
Since this is an IEnumerable
, the easiest way to convert to an IEnumerable<>
is to use OfType
, like this:
var sortedElements = iterator.OfType<XPathNavigator>();
This will not give you XElement
objects; that's part of a separate XML API. However, the XPathNavigator
objects may have the data you need, so you can follow up with some sort of Select
depending on what you need:
var elementValues = iterator.OfType<XPathNavigator>().Select(n => n.Value);
If you do want to use System.Linq.Xml
stuff, you may be able to rewrite your XPath as LINQ on an XDocument
instead.
Upvotes: 2