Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > xml nodes and getting random node

Reply
Thread Tools

xml nodes and getting random node

 
 
=?Utf-8?B?cGVsZWdrMQ==?=
Guest
Posts: n/a
 
      07-31-2007
i have an xml for example:
<xml>
<data>
<var>xxx</var>
</data>
<tip>
<tips active="1">345</tips>
<tips active="1">retertert</tips>
<tips active="1">fdgdfg</tips>
<tips active="0">ert435345</tips>
<tips active="1">fdgdf</tips>
<tips active="1">dfgdfg</tips>
<tips active="0">gdfgdfg</tips>
<tips active="1">dfgdfg</tips>
<tips active="1">dfgdfg</tips>
</tip>
</xml>

and i want to get only the nodes that active="1" and get from then a radnom
one (to get actually its text() )
how can i do this?
thnaks in advance
peleg
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      08-01-2007
On Aug 1, 12:22 am, pelegk1 <(E-Mail Removed)> wrote:
> i have an xml for example:
> <xml>
> <data>
> <var>xxx</var>
> </data>
> <tip>
> <tips active="1">345</tips>
> <tips active="1">retertert</tips>
> <tips active="1">fdgdfg</tips>
> <tips active="0">ert435345</tips>
> <tips active="1">fdgdf</tips>
> <tips active="1">dfgdfg</tips>
> <tips active="0">gdfgdfg</tips>
> <tips active="1">dfgdfg</tips>
> <tips active="1">dfgdfg</tips>
> </tip>
> </xml>
>
> and i want to get only the nodes that active="1" and get from then a radnom
> one (to get actually its text() )
> how can i do this?
> thnaks in advance
> peleg


Try this

XmlNodeList nodes = xmlDoc.SelectNodes("//tip/tips[@active='1']");

Random r = new Random();
int x = r.Next(0, nodes.Count);
int i = 0;

foreach(XmlNode node in nodes) {
if (i == x) {
here's your code
...
return;

} else {
x++;
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
select nodes with child node A and child node B gplott@yahoo.com XML 1 11-01-2006 09:27 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments