xml nodes and getting random node

G

Guest

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
 
G

Guest

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++;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top