Greta Porretta
Greta Porretta

Reputation: 99

c# foreach in NodesCollections

i have this html

            <div class="form-wrapper">
      <div class="clearfix">
        <div class="row">
          <div class="time-wrapper col-xs-6">
        <div class="row">
              <div class="text-left col-md-6 cols-sm-12">
                <input type="radio" id="flight-return-1" name="flight-return"  data-default-meal="X">
                <div class="">
                  <div class="date pad-left-large-md no-padding-left-xs white-space-nowrap">Za. 06 May. 2017</div>
                </div>
              </div>
              <div class="flight date text-right-md text-left-xs col-md-6 cols-sm-12 pad-right-large">
                      <span>
                      bet                      </span>
                      <span class="time">
                                                  12:10                                              </span>
              </div>

            </div>
          </div>
          <div class="time-wrapper col-xs-6">
            <div class="row">
              <div class="flight date text-md-left text-sm-right no-padding-left col-md-7 cols-sm-12">
                        <span class="time">
                                                      14:25                                                  </span>
                        <span>
                        zeb                        </span>
              </div>
              <div class="price-wrapper col-md-5 cols-sm-12">
                                  <div class="price text-right white-space-nowrap">
                                        <span class="currency symbol">€</span> <span class="integer-part">69</span><span class="decimal-part">,99</span>                                      </div>

                </div>
              </div>

            </div>
          </div>
</div>
</div>
</div>
</div>

Please note that i have multiples <div class="row">inside one . i want to get all the data there i'm using this c# code :

    var node_1 = Doc.DocumentNode.SelectNodes("//div[@class='form-wrapper']").First();
    var ITEM = node_1.SelectNodes("//div[@class='clearfix']");
    foreach (var Node in node_1.SelectNodes("//div[@class='clearfix']"))
    {
        Console.WriteLine(Node.SelectNodes("//span[@class='time']")[1].InnerText.Trim());
    }

I'm only trying to get all the times (there is like 4 class(clearfix) ) so i'm expecting dates like :

14:25
18:25
17:50
13:20

but for some reasons i only get :

14:25
14:25
14:25
14:25

it keeps repeating this i m stuck at this

thanks in advance

Upvotes: 0

Views: 268

Answers (2)

Joon
Joon

Reputation: 2147

The double forward slash in the XPATH of your Console.WriteLine statement ("//span[....") is running out of your current node context and returning the first instance in the whole document that matches your XPATH.

Try to make your second XPATH relative (best way is to debug the code and examine what was returned into the Node variable in the loop)

You could also just iterate the spans directly:

foreach (var spanNode in node_1.SelectNodes("//span[@class='time']"))
{
     Console.WriteLine(spanNode.InnerText.Trim());
}

Upvotes: 1

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3182

you are passing index statically this will be the issue

 Node.SelectNodes("//span[@class='time']")[1].InnerText.Trim()//Here [1] you are passing statically

Upvotes: 0

Related Questions