Re: xml nodes and getting random node
On Aug 1, 12:22 am, pelegk1 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++;
}
}
Date:Wed, 01 Aug 2007 03:35:59 -0700
Author:
|