Reputation: 2877
I am using HTML agility pack and parsing to an array.
The information I am parsing changes and when it changes below a certain level I get unhandled exceptions because I am trying to bind the an element [][] that isn't there.
How would I setup error checking to make sure if the array isn't there it wouldn't throw an Unhandled expection?
Eg... If I use the below code and there is no [2][1] then I get an exception, but the html changes so It needs to cope with null dor non existant arrays elements
//first line
textBlock1.Text = node[0][0];
textBlock2.Text = node[0][1];
textBlock3.Text = node[0][2];
//first line
textBlock4.Text = node[1][0];
textBlock5.Text = node[1][1];
textBlock6.Text = node[1][2];
//first line
textBlock7.Text = node[2][0];
textBlock8.Text = node[2][1];
textBlock9.Text = node[2][2];
Array is from this code:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var html = e.Result;
var doc = new HtmlDocument();
doc.LoadHtml(html);
var list = doc.DocumentNode.Descendants("div").ToList();
var node = doc.DocumentNode.Descendants("table")
.FirstOrDefault(x => x.Id == "departures")
.Element("tbody")
.Elements("tr")
.Select(tr => tr.Elements("td").Select(td => td.InnerText).ToArray())
.ToArray();
Upvotes: 0
Views: 108
Reputation: 3827
You can check length for both dimensions, e.g.
if (node.Length > 2)
{
//first line
if (node[2].Length > 0)
{
textBlock7.Text = node[2][0];
}
}
Upvotes: 1