Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sean Dooley 289 posts 528 karma points
    May 05, 2011 @ 11:56
    Sean Dooley
    0

    Cannot convert XPathNavigator to XPathNodeIterator

    I am currently developing an Xslt extensions that randomly orders nodes.

    I'm using the following approach, which works perfectly, no issues.

    public static XPathNodeIterator RandomSet(XPathNodeIterator nodeset)
    {
    try
    {
    Random random = new Random();

    List<int> numbers = new List<int>(nodeset.Count);
    for (int x = 1; x <= nodeset.Count; x++)
    numbers.Add(x);

    List<int> randNumbers = new List<int>(nodeset.Count);
    for (int x = 0; x < nodeset.Count; x++)
    {
    int rn = random.Next(numbers.Count);
    int rnVal = numbers[rn];
    randNumbers.Add(rnVal);
    numbers.RemoveAt(rn);
    }

    XmlDocument xd = new XmlDocument();
    xd.LoadXml("<values />");
    foreach (int i in randNumbers)
    {
    XmlNode thisNumber = xd.CreateElement("value");
    thisNumber.AppendChild(xd.CreateTextNode(i.ToString()));
    xd.DocumentElement.AppendChild(thisNumber);
    }

    return xd.CreateNavigator().Select(".");
    }
    catch
    {
    return nodeset;
    }
    }

    I am also trying to implement randomly ordering nodes using the approach below, which I feel is cleaner, but is causing the following error.

    Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<System.Xml.XPath.XPathNavigator>' to 'System.Xml.XPath.XPathNodeIterator'

    public static XPathNodeIterator RandomLinq(XPathNodeIterator nodeset)
    {
    try
    {
    var randomNodeset = from XPathNavigator node in nodeset
    orderby Guid.NewGuid()
    select node;

    return (XPathNodeIterator)randomNodeset;
    }
    catch
    {
    return nodeset;
    }
    }

    Any ideas how to resolve the error?

Please Sign in or register to post replies

Write your reply to:

Draft