Hello all,
I’m trying to shuffle my Xml doc. What I do is load a xml file into
XmlDocument and have it return a XmlNode object represent my Xml doc.
XmlNode node = doc.DocumentElement;
The Xml file look like:
<control id=”ctr1”>
<action id=”act1”>
<action id=”act2”>
</control>
<control id=”ctr2”>
<action id=”act3”>
<action id=”act4”>
</control>
Then I pass the node returned by the XmlDocument object to
“TestCaseShuffling” method and call it recursively.
The method:
//I’m implementing Fisher-Yates algorithm for the shuffling
private void TestCaseShuffling(XmlNode node, Random rand)
{
int pCount = node.ChildNodes.Count;
if(pCount > 1)
{
XmlNode temp;//node to swap
XmlNode temp2;//node to swap
for(int i = 0; i < pCount; i++)
{
int r = rand.Next(i,pCount);
temp = node.ChildNodes[r].Clone();
temp2 = node.ChildNodes[i].Clone();
node.ReplaceChild(temp2,node.ChildNodes[r]);
node.ReplaceChild(temp,node.ChildNodes[i]);
}
}
}
The produced result:
<control id=”ctr2”>
<action id=”act4”>
<action id=”act3”>
</control>
<control id=”ctr1”>
<action id=”act2”>
<action id=”act1”>
</control>
I’m thinking using XpathNavigator instead of working with XmlNode directly.
Would this be more effective? Or any one have any better idea?

Signature
Regards Peter
Martin Honnen - 01 Mar 2008 13:01 GMT
> I’m trying to shuffle my Xml doc. What I do is load a xml file into
> XmlDocument and have it return a XmlNode object represent my Xml doc.
[quoted text clipped - 8 lines]
> <action id=”act4”>
> </control>
That snippet is not well-formed at all, it has the wrong quote
characters, it has no root element, the action elements have no end tag.
> I’m thinking using XpathNavigator instead of working with XmlNode directly.
> Would this be more effective? Or any one have any better idea?
XmlDocument/XmlNode is the right tool if you want to manipulate XML.
Using an XPathNavigator created from an XmlDocument or XmlNode is
possible but I don't think it is more effective, it is just a different API.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
vhepeter2005 - 02 Mar 2008 15:05 GMT