Search code examples
c#xsltxpathxpathnavigatorxpathnodeiterator

How do i loop thourgh XPathNodeIterator and randomize its children?


As per title, i need to create a method in C#/.NET 4 that can jumble the order a XPathNodeIterator object. The method needs to loop though all the child items ("/*") to randomly reorder the children.

I need some help to some to get started - All i have is the method stub:

public XPathNodeIterator RandomizeXPathNodeIterator(XPathNodeIterator iterator)

Any help is appreciated! Thanks


Solution

  • You can iterate XPathNodeIterator as:

        XPathNavigator[] positions = new XPathNavigator[iterator.Count];
        int i = 0;
    
        while (iterator.MoveNext())
        {
            XPathNavigator n = iterator.Current;
            positions[i] = n;
    
            i++;
        }
    

    Then, scrumble the array