Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2008

Tip: Looking for answers? Try searching our database.

XpathNavigtor or XmlNode which one is better?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vhepeter2005 - 29 Feb 2008 21:57 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.