xml nodes and getting random node

Discussion in 'ASP .Net' started by =?Utf-8?B?cGVsZWdrMQ==?=, Jul 31, 2007.

  1. 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
     
    =?Utf-8?B?cGVsZWdrMQ==?=, Jul 31, 2007
    #1
    1. Advertising

  2. 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++;
    }
    }
     
    Alexey Smirnov, Aug 1, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,648
  2. gavnosis
    Replies:
    0
    Views:
    544
    gavnosis
    Aug 2, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,506
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    1
    Views:
    1,234
    Joe Kesselman
    Nov 1, 2006
  5. VK
    Replies:
    15
    Views:
    1,322
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page