I don't know what is wrong with this code. I can't see result in textBox. Somebody can check if the XPath is correct? And where is mistake??
namespace Pogoda { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public static bool TryGetTemperature(HtmlAgilityPack.HtmlDocument doc, out int temperature) { temperature = 0;
var temp = doc.DocumentNode.SelectSingleNode(
"//div(@class, 'temperature')]/div[contains(@class, 'temp')]");
return temp != null ?
int.TryParse(temp.InnerText.TrimEnd('°'), out temperature) :
false;
}
private void btnOnet_Click(object sender, EventArgs e)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
HtmlWeb web = new HtmlWeb();
doc = web.Load("https://pogoda.onet.pl");
if (TryGetTemperature(doc, out int temperature))
{
onet.Text = temperature.ToString();
}
else
{
MessageBox.Show("Error");
}
}
}
}