0

I am parsing a website using HtmlAgilityPack for android in Xamarin. I know there is the first() keywords but does anyone know how I would be able to access the second instance of html text? For instance, I only would like to see "Arrival Predictions not available at this time" as shown in the app sample picture.

app sample picture

void Btn_Click(object sender, System.EventArgs e)
{
    HtmlWeb hw = new HtmlWeb();
    //stores site in a document object of HTMLDocument class
    HtmlDocument document = hw.Load("https://broncoshuttle.com/simple/routes/3164/stops/36359");
    HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//ul//li[contains(.,'')] ");
    string result = "";
    foreach( var item in nodes)
    {
        result += item.InnerText;
    }

    MyTextView.Text = result;
}
M Reza
  • 18,350
  • 14
  • 66
  • 71

1 Answers1

1

If I had control over HTML I would probably use HTML class like error-massage to make HTML markup more semantic and search with more precision. Currently you can use simple HtmlNodeCollection indexer or Enumerable.Skip() and Enumerable.Take() Linq extension methods:

// errpr-message HTML class and SelectSingleNode().
HtmlNode error = document.DocumentNode.SelectSingleNode(@"//*[contains(concat("" "", normalize-space(@class), "" ""), "" error-message "")]");
// HtmlNodeCollection indexer.
HtmlNode error = nodes[1];
// Linq.
HtmlNode error = nodes.Skip(1).Take(1).SingleOrDefault();

For more information check:

Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50