public BarchartParser()
{
// Initialize list
StockSymbols = new List<string>();
// Add items
ParseBarchart();
}
this is the C'tor that calls the method
private async void ParseBarchart()
{
try
{
#region Get Html Document
// Get response from site
HttpClient http = new HttpClient();
var response = await http.GetByteArrayAsync(BARCHART_WEBSITE);
/* Break or W/e happens on this line ^^^ */
// Encode html response to UTF-8
string source = Encoding.GetEncoding(BARCHART_ENCODING)
.GetString(response, 0, response.Length - 1);
// Get html
HtmlDocument document = new HtmlDocument();
document.LoadHtml(source);
#endregion
#region Get Data From Table
// Get table containining stock info
HtmlNode table = document.DocumentNode.Descendants()
.Single<HtmlNode>
(
x => (x.Name == "table") &&
(x.Attributes["class"] != null) &&
(x.Attributes["class"].Value.Equals("datatable ajax")) &&
(x.Attributes["id"].Value.Equals("dt1"))
);
// Get 'tbody' element from table
HtmlNode tbody = table.Descendants("tbody").FirstOrDefault();
// Get all rows from the table
List<HtmlNode> allStocks = tbody.Descendants("tr").ToList();
// For each row, id is "td1_X" where X is the symbol of the stock
foreach (HtmlNode row in allStocks)
{
StockSymbols.Add(row.Attributes["id"].Value.ToString()
.Split(new char[] { '_' })[1]);
}
#endregion
}
catch
{
StockSymbols = new List<string>();
StockSymbols.Add("this didn't work");
}
}
And the code from a simple form application that uses this:
BarchartParser barchartData;
public Form1()
{
InitializeComponent();
barchartData = new BarchartParser();
}
private void Form1_Load(object sender, EventArgs e)
{
if (barchartData.StockSymbols != null && barchartData.StockSymbols.Count > 0)
MessageBox.Show(barchartData.StockSymbols[0]);
else
MessageBox.Show("barchartData.StockSymbols is null or count == 0");
this.Close();
}
Not exactly sure what's going on here. It worked for one time that I debugged and then it stopped working.
This code is part of a function that is called during a C'tor. When this throw or whatever happens,
It just continues to the next breakpoint that I set in debug mode... Anyone has a clue of what
May be the cause of this?
Edit: I know it's not a throw because the code in the catch block doesn't happen. It simply moves on
Just in general, i'm following this guide https://code.msdn.microsoft.com/Parsing-Html-using-C-721be358/sourcecode?fileId=122353&pathId=1834557721