0

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();
3
  • Is this a multidimensional array or a jagged array? You imply that it's multidimensional, but in that case if [2][1] is not valid it means that all of the third row or the second column will not be valid. Commented Sep 15, 2011 at 7:43
  • Can you add some more code? I can't tell from the code you've posted it's an array - can be just types with indexers Commented Sep 15, 2011 at 7:48
  • @jon its a multidimensional array. The array size is based on HTML parse, os sometimes all the rows are not valid and thats what I want to error check. Check if the array exists before binding. Commented Sep 15, 2011 at 9:13

1 Answer 1

1

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];
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Using a conditional? Or how would I implement?
Thanks for the suggestion, but it wont let me use node[0] as it is an [][] array.
I've edited again :-) What is the exact type of node? is it really multidimensional array, or just some type with indexers?
Thanks for your help. but still not working, error: operator && cannot be applied to Bool or int. The array is a combination. It is a list of the 3 values, each line has three values. PLacename, number, number.
Ok, it is an multi-dimensional array :-) fixed my code last time, next time I should be using compiler :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.